| OLD | NEW |
| (Empty) |
| 1 // 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 #include "net/quic/quic_framer.h" | |
| 6 | |
| 7 #include <cstdint> | |
| 8 #include <memory> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/stl_util.h" | |
| 14 #include "net/quic/crypto/crypto_framer.h" | |
| 15 #include "net/quic/crypto/crypto_handshake_message.h" | |
| 16 #include "net/quic/crypto/crypto_protocol.h" | |
| 17 #include "net/quic/crypto/quic_decrypter.h" | |
| 18 #include "net/quic/crypto/quic_encrypter.h" | |
| 19 #include "net/quic/quic_bug_tracker.h" | |
| 20 #include "net/quic/quic_data_reader.h" | |
| 21 #include "net/quic/quic_data_writer.h" | |
| 22 #include "net/quic/quic_flags.h" | |
| 23 #include "net/quic/quic_socket_address_coder.h" | |
| 24 #include "net/quic/quic_utils.h" | |
| 25 | |
| 26 using base::StringPiece; | |
| 27 using std::map; | |
| 28 using std::max; | |
| 29 using std::min; | |
| 30 using std::numeric_limits; | |
| 31 using std::string; | |
| 32 using std::vector; | |
| 33 #define PREDICT_FALSE(x) (x) | |
| 34 | |
| 35 namespace net { | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 // Mask to select the lowest 48 bits of a packet number. | |
| 40 const QuicPacketNumber k6ByteSequenceNumberMask = UINT64_C(0x0000FFFFFFFFFFFF); | |
| 41 const QuicPacketNumber k4ByteSequenceNumberMask = UINT64_C(0x00000000FFFFFFFF); | |
| 42 const QuicPacketNumber k2ByteSequenceNumberMask = UINT64_C(0x000000000000FFFF); | |
| 43 const QuicPacketNumber k1ByteSequenceNumberMask = UINT64_C(0x00000000000000FF); | |
| 44 | |
| 45 // Number of bits the packet number length bits are shifted from the right | |
| 46 // edge of the public header. | |
| 47 const uint8_t kPublicHeaderSequenceNumberShift = 4; | |
| 48 | |
| 49 // New Frame Types, QUIC v. >= 10: | |
| 50 // There are two interpretations for the Frame Type byte in the QUIC protocol, | |
| 51 // resulting in two Frame Types: Special Frame Types and Regular Frame Types. | |
| 52 // | |
| 53 // Regular Frame Types use the Frame Type byte simply. Currently defined | |
| 54 // Regular Frame Types are: | |
| 55 // Padding : 0b 00000000 (0x00) | |
| 56 // ResetStream : 0b 00000001 (0x01) | |
| 57 // ConnectionClose : 0b 00000010 (0x02) | |
| 58 // GoAway : 0b 00000011 (0x03) | |
| 59 // WindowUpdate : 0b 00000100 (0x04) | |
| 60 // Blocked : 0b 00000101 (0x05) | |
| 61 // | |
| 62 // Special Frame Types encode both a Frame Type and corresponding flags | |
| 63 // all in the Frame Type byte. Currently defined Special Frame Types are: | |
| 64 // Stream : 0b 1xxxxxxx | |
| 65 // Ack : 0b 01xxxxxx | |
| 66 // | |
| 67 // Semantics of the flag bits above (the x bits) depends on the frame type. | |
| 68 | |
| 69 // Masks to determine if the frame type is a special use | |
| 70 // and for specific special frame types. | |
| 71 const uint8_t kQuicFrameTypeSpecialMask = 0xE0; // 0b 11100000 | |
| 72 const uint8_t kQuicFrameTypeStreamMask = 0x80; | |
| 73 const uint8_t kQuicFrameTypeAckMask = 0x40; | |
| 74 | |
| 75 // Stream frame relative shifts and masks for interpreting the stream flags. | |
| 76 // StreamID may be 1, 2, 3, or 4 bytes. | |
| 77 const uint8_t kQuicStreamIdShift = 2; | |
| 78 const uint8_t kQuicStreamIDLengthMask = 0x03; | |
| 79 | |
| 80 // Offset may be 0, 2, 3, 4, 5, 6, 7, 8 bytes. | |
| 81 const uint8_t kQuicStreamOffsetShift = 3; | |
| 82 const uint8_t kQuicStreamOffsetMask = 0x07; | |
| 83 | |
| 84 // Data length may be 0 or 2 bytes. | |
| 85 const uint8_t kQuicStreamDataLengthShift = 1; | |
| 86 const uint8_t kQuicStreamDataLengthMask = 0x01; | |
| 87 | |
| 88 // Fin bit may be set or not. | |
| 89 const uint8_t kQuicStreamFinShift = 1; | |
| 90 const uint8_t kQuicStreamFinMask = 0x01; | |
| 91 | |
| 92 // packet number size shift used in AckFrames. | |
| 93 const uint8_t kQuicSequenceNumberLengthShift = 2; | |
| 94 | |
| 95 // Acks may be truncated. | |
| 96 const uint8_t kQuicAckTruncatedShift = 1; | |
| 97 const uint8_t kQuicAckTruncatedMask = 0x01; | |
| 98 | |
| 99 // Acks may not have any nacks. | |
| 100 const uint8_t kQuicHasNacksMask = 0x01; | |
| 101 // Acks may have only one ack block. | |
| 102 const uint8_t kQuicHasMultipleAckBlocksMask = 0x01; | |
| 103 const uint8_t kQuicHasMultipleAckBlocksShift = 1; | |
| 104 | |
| 105 // Returns the absolute value of the difference between |a| and |b|. | |
| 106 QuicPacketNumber Delta(QuicPacketNumber a, QuicPacketNumber b) { | |
| 107 // Since these are unsigned numbers, we can't just return abs(a - b) | |
| 108 if (a < b) { | |
| 109 return b - a; | |
| 110 } | |
| 111 return a - b; | |
| 112 } | |
| 113 | |
| 114 QuicPacketNumber ClosestTo(QuicPacketNumber target, | |
| 115 QuicPacketNumber a, | |
| 116 QuicPacketNumber b) { | |
| 117 return (Delta(target, a) < Delta(target, b)) ? a : b; | |
| 118 } | |
| 119 | |
| 120 QuicPacketNumberLength ReadSequenceNumberLength(uint8_t flags) { | |
| 121 switch (flags & PACKET_FLAGS_6BYTE_PACKET) { | |
| 122 case PACKET_FLAGS_6BYTE_PACKET: | |
| 123 return PACKET_6BYTE_PACKET_NUMBER; | |
| 124 case PACKET_FLAGS_4BYTE_PACKET: | |
| 125 return PACKET_4BYTE_PACKET_NUMBER; | |
| 126 case PACKET_FLAGS_2BYTE_PACKET: | |
| 127 return PACKET_2BYTE_PACKET_NUMBER; | |
| 128 case PACKET_FLAGS_1BYTE_PACKET: | |
| 129 return PACKET_1BYTE_PACKET_NUMBER; | |
| 130 default: | |
| 131 QUIC_BUG << "Unreachable case statement."; | |
| 132 return PACKET_6BYTE_PACKET_NUMBER; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 } // namespace | |
| 137 | |
| 138 QuicFramer::QuicFramer(const QuicVersionVector& supported_versions, | |
| 139 QuicTime creation_time, | |
| 140 Perspective perspective) | |
| 141 : visitor_(nullptr), | |
| 142 entropy_calculator_(nullptr), | |
| 143 error_(QUIC_NO_ERROR), | |
| 144 last_packet_number_(0), | |
| 145 last_path_id_(kInvalidPathId), | |
| 146 last_serialized_connection_id_(0), | |
| 147 supported_versions_(supported_versions), | |
| 148 decrypter_level_(ENCRYPTION_NONE), | |
| 149 alternative_decrypter_level_(ENCRYPTION_NONE), | |
| 150 alternative_decrypter_latch_(false), | |
| 151 perspective_(perspective), | |
| 152 validate_flags_(true), | |
| 153 creation_time_(creation_time), | |
| 154 last_timestamp_(QuicTime::Delta::Zero()) { | |
| 155 DCHECK(!supported_versions.empty()); | |
| 156 quic_version_ = supported_versions_[0]; | |
| 157 decrypter_.reset(QuicDecrypter::Create(kNULL)); | |
| 158 encrypter_[ENCRYPTION_NONE].reset(QuicEncrypter::Create(kNULL)); | |
| 159 } | |
| 160 | |
| 161 QuicFramer::~QuicFramer() {} | |
| 162 | |
| 163 // static | |
| 164 size_t QuicFramer::GetMinStreamFrameSize(QuicStreamId stream_id, | |
| 165 QuicStreamOffset offset, | |
| 166 bool last_frame_in_packet) { | |
| 167 return kQuicFrameTypeSize + GetStreamIdSize(stream_id) + | |
| 168 GetStreamOffsetSize(offset) + | |
| 169 (last_frame_in_packet ? 0 : kQuicStreamPayloadLengthSize); | |
| 170 } | |
| 171 | |
| 172 // static | |
| 173 size_t QuicFramer::GetMinAckFrameSize( | |
| 174 QuicVersion version, | |
| 175 QuicPacketNumberLength largest_observed_length) { | |
| 176 size_t min_size = kQuicFrameTypeSize + largest_observed_length + | |
| 177 kQuicDeltaTimeLargestObservedSize; | |
| 178 if (version <= QUIC_VERSION_33) { | |
| 179 return min_size + kQuicEntropyHashSize; | |
| 180 } | |
| 181 return min_size + kQuicNumTimestampsSize; | |
| 182 } | |
| 183 | |
| 184 // static | |
| 185 size_t QuicFramer::GetStopWaitingFrameSize( | |
| 186 QuicVersion version, | |
| 187 QuicPacketNumberLength packet_number_length) { | |
| 188 size_t min_size = kQuicFrameTypeSize + packet_number_length; | |
| 189 if (version <= QUIC_VERSION_33) { | |
| 190 return min_size + kQuicEntropyHashSize; | |
| 191 } | |
| 192 return min_size; | |
| 193 } | |
| 194 | |
| 195 // static | |
| 196 size_t QuicFramer::GetRstStreamFrameSize() { | |
| 197 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize + | |
| 198 kQuicErrorCodeSize; | |
| 199 } | |
| 200 | |
| 201 // static | |
| 202 size_t QuicFramer::GetMinConnectionCloseFrameSize() { | |
| 203 return kQuicFrameTypeSize + kQuicErrorCodeSize + kQuicErrorDetailsLengthSize; | |
| 204 } | |
| 205 | |
| 206 // static | |
| 207 size_t QuicFramer::GetMinGoAwayFrameSize() { | |
| 208 return kQuicFrameTypeSize + kQuicErrorCodeSize + kQuicErrorDetailsLengthSize + | |
| 209 kQuicMaxStreamIdSize; | |
| 210 } | |
| 211 | |
| 212 // static | |
| 213 size_t QuicFramer::GetWindowUpdateFrameSize() { | |
| 214 return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize; | |
| 215 } | |
| 216 | |
| 217 // static | |
| 218 size_t QuicFramer::GetBlockedFrameSize() { | |
| 219 return kQuicFrameTypeSize + kQuicMaxStreamIdSize; | |
| 220 } | |
| 221 | |
| 222 // static | |
| 223 size_t QuicFramer::GetPathCloseFrameSize() { | |
| 224 return kQuicFrameTypeSize + kQuicPathIdSize; | |
| 225 } | |
| 226 | |
| 227 // static | |
| 228 size_t QuicFramer::GetStreamIdSize(QuicStreamId stream_id) { | |
| 229 // Sizes are 1 through 4 bytes. | |
| 230 for (int i = 1; i <= 4; ++i) { | |
| 231 stream_id >>= 8; | |
| 232 if (stream_id == 0) { | |
| 233 return i; | |
| 234 } | |
| 235 } | |
| 236 QUIC_BUG << "Failed to determine StreamIDSize."; | |
| 237 return 4; | |
| 238 } | |
| 239 | |
| 240 // static | |
| 241 size_t QuicFramer::GetStreamOffsetSize(QuicStreamOffset offset) { | |
| 242 // 0 is a special case. | |
| 243 if (offset == 0) { | |
| 244 return 0; | |
| 245 } | |
| 246 // 2 through 8 are the remaining sizes. | |
| 247 offset >>= 8; | |
| 248 for (int i = 2; i <= 8; ++i) { | |
| 249 offset >>= 8; | |
| 250 if (offset == 0) { | |
| 251 return i; | |
| 252 } | |
| 253 } | |
| 254 QUIC_BUG << "Failed to determine StreamOffsetSize."; | |
| 255 return 8; | |
| 256 } | |
| 257 | |
| 258 // static | |
| 259 size_t QuicFramer::GetVersionNegotiationPacketSize(size_t number_versions) { | |
| 260 return kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID + | |
| 261 number_versions * kQuicVersionSize; | |
| 262 } | |
| 263 | |
| 264 bool QuicFramer::IsSupportedVersion(const QuicVersion version) const { | |
| 265 for (size_t i = 0; i < supported_versions_.size(); ++i) { | |
| 266 if (version == supported_versions_[i]) { | |
| 267 return true; | |
| 268 } | |
| 269 } | |
| 270 return false; | |
| 271 } | |
| 272 | |
| 273 size_t QuicFramer::GetSerializedFrameLength( | |
| 274 const QuicFrame& frame, | |
| 275 size_t free_bytes, | |
| 276 bool first_frame, | |
| 277 bool last_frame, | |
| 278 QuicPacketNumberLength packet_number_length) { | |
| 279 // Prevent a rare crash reported in b/19458523. | |
| 280 if ((frame.type == STREAM_FRAME || frame.type == ACK_FRAME) && | |
| 281 frame.stream_frame == nullptr) { | |
| 282 QUIC_BUG << "Cannot compute the length of a null frame. " | |
| 283 << "type:" << frame.type << "free_bytes:" << free_bytes | |
| 284 << " first_frame:" << first_frame << " last_frame:" << last_frame | |
| 285 << " seq num length:" << packet_number_length; | |
| 286 set_error(QUIC_INTERNAL_ERROR); | |
| 287 visitor_->OnError(this); | |
| 288 return 0; | |
| 289 } | |
| 290 if (frame.type == PADDING_FRAME) { | |
| 291 if (frame.padding_frame.num_padding_bytes == -1) { | |
| 292 // Full padding to the end of the packet. | |
| 293 return free_bytes; | |
| 294 } else { | |
| 295 // Lite padding. | |
| 296 return free_bytes < | |
| 297 static_cast<size_t>(frame.padding_frame.num_padding_bytes) | |
| 298 ? free_bytes | |
| 299 : frame.padding_frame.num_padding_bytes; | |
| 300 } | |
| 301 } | |
| 302 | |
| 303 size_t frame_len = | |
| 304 ComputeFrameLength(frame, last_frame, packet_number_length); | |
| 305 if (frame_len <= free_bytes) { | |
| 306 // Frame fits within packet. Note that acks may be truncated. | |
| 307 return frame_len; | |
| 308 } | |
| 309 // Only truncate the first frame in a packet, so if subsequent ones go | |
| 310 // over, stop including more frames. | |
| 311 if (!first_frame) { | |
| 312 return 0; | |
| 313 } | |
| 314 bool can_truncate = | |
| 315 frame.type == ACK_FRAME && | |
| 316 free_bytes >= | |
| 317 GetMinAckFrameSize(quic_version_, PACKET_6BYTE_PACKET_NUMBER); | |
| 318 if (can_truncate) { | |
| 319 // Truncate the frame so the packet will not exceed kMaxPacketSize. | |
| 320 // Note that we may not use every byte of the writer in this case. | |
| 321 DVLOG(1) << "Truncating large frame, free bytes: " << free_bytes; | |
| 322 return free_bytes; | |
| 323 } | |
| 324 return 0; | |
| 325 } | |
| 326 | |
| 327 QuicFramer::AckFrameInfo::AckFrameInfo() : max_delta(0) {} | |
| 328 | |
| 329 QuicFramer::AckFrameInfo::AckFrameInfo(const AckFrameInfo& other) = default; | |
| 330 | |
| 331 QuicFramer::AckFrameInfo::~AckFrameInfo() {} | |
| 332 | |
| 333 QuicFramer::AckBlock::AckBlock(uint8_t gap, QuicPacketNumber length) | |
| 334 : gap(gap), length(length) {} | |
| 335 | |
| 336 QuicFramer::AckBlock::AckBlock(const AckBlock& other) = default; | |
| 337 | |
| 338 QuicFramer::AckBlock::~AckBlock() {} | |
| 339 | |
| 340 QuicFramer::NewAckFrameInfo::NewAckFrameInfo() | |
| 341 : max_block_length(0), first_block_length(0), num_ack_blocks(0) {} | |
| 342 | |
| 343 QuicFramer::NewAckFrameInfo::NewAckFrameInfo(const NewAckFrameInfo& other) = | |
| 344 default; | |
| 345 | |
| 346 QuicFramer::NewAckFrameInfo::~NewAckFrameInfo() {} | |
| 347 | |
| 348 // static | |
| 349 QuicPacketEntropyHash QuicFramer::GetPacketEntropyHash( | |
| 350 const QuicPacketHeader& header) { | |
| 351 return header.entropy_flag << (header.packet_number % 8); | |
| 352 } | |
| 353 | |
| 354 size_t QuicFramer::BuildDataPacket(const QuicPacketHeader& header, | |
| 355 const QuicFrames& frames, | |
| 356 char* buffer, | |
| 357 size_t packet_length) { | |
| 358 QuicDataWriter writer(packet_length, buffer); | |
| 359 if (!AppendPacketHeader(header, &writer)) { | |
| 360 QUIC_BUG << "AppendPacketHeader failed"; | |
| 361 return 0; | |
| 362 } | |
| 363 | |
| 364 size_t i = 0; | |
| 365 for (const QuicFrame& frame : frames) { | |
| 366 // Determine if we should write stream frame length in header. | |
| 367 const bool no_stream_frame_length = i == frames.size() - 1; | |
| 368 if (!AppendTypeByte(frame, no_stream_frame_length, &writer)) { | |
| 369 QUIC_BUG << "AppendTypeByte failed"; | |
| 370 return 0; | |
| 371 } | |
| 372 | |
| 373 switch (frame.type) { | |
| 374 case PADDING_FRAME: | |
| 375 writer.WritePadding(); | |
| 376 break; | |
| 377 case STREAM_FRAME: | |
| 378 if (!AppendStreamFrame(*frame.stream_frame, no_stream_frame_length, | |
| 379 &writer)) { | |
| 380 QUIC_BUG << "AppendStreamFrame failed"; | |
| 381 return 0; | |
| 382 } | |
| 383 break; | |
| 384 case ACK_FRAME: | |
| 385 if (quic_version_ <= QUIC_VERSION_33) { | |
| 386 if (!AppendAckFrameAndTypeByte(header, *frame.ack_frame, &writer)) { | |
| 387 QUIC_BUG << "AppendAckFrameAndTypeByte failed" | |
| 388 << " header: " << header | |
| 389 << " ack_fame: " << *frame.ack_frame; | |
| 390 return 0; | |
| 391 } | |
| 392 } else { | |
| 393 if (!AppendNewAckFrameAndTypeByte(*frame.ack_frame, &writer)) { | |
| 394 QUIC_BUG << "AppendNewAckFrameAndTypeByte failed"; | |
| 395 return 0; | |
| 396 } | |
| 397 } | |
| 398 break; | |
| 399 case STOP_WAITING_FRAME: | |
| 400 if (!AppendStopWaitingFrame(header, *frame.stop_waiting_frame, | |
| 401 &writer)) { | |
| 402 QUIC_BUG << "AppendStopWaitingFrame failed"; | |
| 403 return 0; | |
| 404 } | |
| 405 break; | |
| 406 case MTU_DISCOVERY_FRAME: | |
| 407 // MTU discovery frames are serialized as ping frames. | |
| 408 case PING_FRAME: | |
| 409 // Ping has no payload. | |
| 410 break; | |
| 411 case RST_STREAM_FRAME: | |
| 412 if (!AppendRstStreamFrame(*frame.rst_stream_frame, &writer)) { | |
| 413 QUIC_BUG << "AppendRstStreamFrame failed"; | |
| 414 return 0; | |
| 415 } | |
| 416 break; | |
| 417 case CONNECTION_CLOSE_FRAME: | |
| 418 if (!AppendConnectionCloseFrame(*frame.connection_close_frame, | |
| 419 &writer)) { | |
| 420 QUIC_BUG << "AppendConnectionCloseFrame failed"; | |
| 421 return 0; | |
| 422 } | |
| 423 break; | |
| 424 case GOAWAY_FRAME: | |
| 425 if (!AppendGoAwayFrame(*frame.goaway_frame, &writer)) { | |
| 426 QUIC_BUG << "AppendGoAwayFrame failed"; | |
| 427 return 0; | |
| 428 } | |
| 429 break; | |
| 430 case WINDOW_UPDATE_FRAME: | |
| 431 if (!AppendWindowUpdateFrame(*frame.window_update_frame, &writer)) { | |
| 432 QUIC_BUG << "AppendWindowUpdateFrame failed"; | |
| 433 return 0; | |
| 434 } | |
| 435 break; | |
| 436 case BLOCKED_FRAME: | |
| 437 if (!AppendBlockedFrame(*frame.blocked_frame, &writer)) { | |
| 438 QUIC_BUG << "AppendBlockedFrame failed"; | |
| 439 return 0; | |
| 440 } | |
| 441 break; | |
| 442 case PATH_CLOSE_FRAME: | |
| 443 if (!AppendPathCloseFrame(*frame.path_close_frame, &writer)) { | |
| 444 QUIC_BUG << "AppendPathCloseFrame failed"; | |
| 445 return 0; | |
| 446 } | |
| 447 break; | |
| 448 default: | |
| 449 RaiseError(QUIC_INVALID_FRAME_DATA); | |
| 450 QUIC_BUG << "QUIC_INVALID_FRAME_DATA"; | |
| 451 return 0; | |
| 452 } | |
| 453 ++i; | |
| 454 } | |
| 455 | |
| 456 return writer.length(); | |
| 457 } | |
| 458 | |
| 459 // static | |
| 460 QuicEncryptedPacket* QuicFramer::BuildPublicResetPacket( | |
| 461 const QuicPublicResetPacket& packet) { | |
| 462 DCHECK(packet.public_header.reset_flag); | |
| 463 | |
| 464 CryptoHandshakeMessage reset; | |
| 465 reset.set_tag(kPRST); | |
| 466 reset.SetValue(kRNON, packet.nonce_proof); | |
| 467 reset.SetValue(kRSEQ, packet.rejected_packet_number); | |
| 468 if (!packet.client_address.address().empty()) { | |
| 469 // packet.client_address is non-empty. | |
| 470 QuicSocketAddressCoder address_coder(packet.client_address); | |
| 471 string serialized_address = address_coder.Encode(); | |
| 472 if (serialized_address.empty()) { | |
| 473 return nullptr; | |
| 474 } | |
| 475 reset.SetStringPiece(kCADR, serialized_address); | |
| 476 } | |
| 477 const QuicData& reset_serialized = reset.GetSerialized(); | |
| 478 | |
| 479 size_t len = | |
| 480 kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID + reset_serialized.length(); | |
| 481 std::unique_ptr<char[]> buffer(new char[len]); | |
| 482 QuicDataWriter writer(len, buffer.get()); | |
| 483 | |
| 484 uint8_t flags = static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_RST | | |
| 485 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID); | |
| 486 if (FLAGS_quic_use_old_public_reset_packets) { | |
| 487 // TODO(rch): Remove this QUIC_VERSION_32 is retired. | |
| 488 flags |= static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); | |
| 489 } | |
| 490 if (!writer.WriteUInt8(flags)) { | |
| 491 return nullptr; | |
| 492 } | |
| 493 | |
| 494 if (!writer.WriteUInt64(packet.public_header.connection_id)) { | |
| 495 return nullptr; | |
| 496 } | |
| 497 | |
| 498 if (!writer.WriteBytes(reset_serialized.data(), reset_serialized.length())) { | |
| 499 return nullptr; | |
| 500 } | |
| 501 | |
| 502 return new QuicEncryptedPacket(buffer.release(), len, true); | |
| 503 } | |
| 504 | |
| 505 // static | |
| 506 QuicEncryptedPacket* QuicFramer::BuildVersionNegotiationPacket( | |
| 507 QuicConnectionId connection_id, | |
| 508 const QuicVersionVector& versions) { | |
| 509 DCHECK(!versions.empty()); | |
| 510 size_t len = GetVersionNegotiationPacketSize(versions.size()); | |
| 511 std::unique_ptr<char[]> buffer(new char[len]); | |
| 512 QuicDataWriter writer(len, buffer.get()); | |
| 513 | |
| 514 uint8_t flags = static_cast<uint8_t>( | |
| 515 PACKET_PUBLIC_FLAGS_VERSION | PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID | | |
| 516 // TODO(rch): Remove this QUIC_VERSION_32 is retired. | |
| 517 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); | |
| 518 if (!writer.WriteUInt8(flags)) { | |
| 519 return nullptr; | |
| 520 } | |
| 521 | |
| 522 if (!writer.WriteUInt64(connection_id)) { | |
| 523 return nullptr; | |
| 524 } | |
| 525 | |
| 526 for (QuicVersion version : versions) { | |
| 527 if (!writer.WriteUInt32(QuicVersionToQuicTag(version))) { | |
| 528 return nullptr; | |
| 529 } | |
| 530 } | |
| 531 | |
| 532 return new QuicEncryptedPacket(buffer.release(), len, true); | |
| 533 } | |
| 534 | |
| 535 bool QuicFramer::ProcessPacket(const QuicEncryptedPacket& packet) { | |
| 536 QuicDataReader reader(packet.data(), packet.length()); | |
| 537 | |
| 538 visitor_->OnPacket(); | |
| 539 | |
| 540 // First parse the public header. | |
| 541 QuicPacketPublicHeader public_header; | |
| 542 if (!ProcessPublicHeader(&reader, &public_header)) { | |
| 543 DLOG(WARNING) << "Unable to process public header."; | |
| 544 DCHECK_NE("", detailed_error_); | |
| 545 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 546 } | |
| 547 | |
| 548 if (!visitor_->OnUnauthenticatedPublicHeader(public_header)) { | |
| 549 // The visitor suppresses further processing of the packet. | |
| 550 return true; | |
| 551 } | |
| 552 | |
| 553 if (perspective_ == Perspective::IS_SERVER && public_header.version_flag && | |
| 554 public_header.versions[0] != quic_version_) { | |
| 555 if (!visitor_->OnProtocolVersionMismatch(public_header.versions[0])) { | |
| 556 return true; | |
| 557 } | |
| 558 } | |
| 559 | |
| 560 bool rv; | |
| 561 if (perspective_ == Perspective::IS_CLIENT && public_header.version_flag) { | |
| 562 rv = ProcessVersionNegotiationPacket(&reader, &public_header); | |
| 563 } else if (public_header.reset_flag) { | |
| 564 rv = ProcessPublicResetPacket(&reader, public_header); | |
| 565 } else if (packet.length() <= kMaxPacketSize) { | |
| 566 // The optimized decryption algorithm implementations run faster when | |
| 567 // operating on aligned memory. | |
| 568 // | |
| 569 // TODO(rtenneti): Change the default 64 alignas value (used the default | |
| 570 // value from CACHELINE_SIZE). | |
| 571 ALIGNAS(64) char buffer[kMaxPacketSize]; | |
| 572 rv = ProcessDataPacket(&reader, public_header, packet, buffer, | |
| 573 kMaxPacketSize); | |
| 574 } else { | |
| 575 std::unique_ptr<char[]> large_buffer(new char[packet.length()]); | |
| 576 rv = ProcessDataPacket(&reader, public_header, packet, large_buffer.get(), | |
| 577 packet.length()); | |
| 578 QUIC_BUG_IF(rv) << "QUIC should never successfully process packets larger" | |
| 579 << "than kMaxPacketSize. packet size:" << packet.length(); | |
| 580 } | |
| 581 | |
| 582 return rv; | |
| 583 } | |
| 584 | |
| 585 bool QuicFramer::ProcessVersionNegotiationPacket( | |
| 586 QuicDataReader* reader, | |
| 587 QuicPacketPublicHeader* public_header) { | |
| 588 DCHECK_EQ(Perspective::IS_CLIENT, perspective_); | |
| 589 // Try reading at least once to raise error if the packet is invalid. | |
| 590 do { | |
| 591 QuicTag version; | |
| 592 if (!reader->ReadBytes(&version, kQuicVersionSize)) { | |
| 593 set_detailed_error("Unable to read supported version in negotiation."); | |
| 594 return RaiseError(QUIC_INVALID_VERSION_NEGOTIATION_PACKET); | |
| 595 } | |
| 596 public_header->versions.push_back(QuicTagToQuicVersion(version)); | |
| 597 } while (!reader->IsDoneReading()); | |
| 598 | |
| 599 visitor_->OnVersionNegotiationPacket(*public_header); | |
| 600 return true; | |
| 601 } | |
| 602 | |
| 603 bool QuicFramer::ProcessDataPacket(QuicDataReader* encrypted_reader, | |
| 604 const QuicPacketPublicHeader& public_header, | |
| 605 const QuicEncryptedPacket& packet, | |
| 606 char* decrypted_buffer, | |
| 607 size_t buffer_length) { | |
| 608 QuicPacketHeader header(public_header); | |
| 609 if (!ProcessUnauthenticatedHeader(encrypted_reader, &header)) { | |
| 610 DLOG(WARNING) << "Unable to process packet header. Stopping parsing."; | |
| 611 return false; | |
| 612 } | |
| 613 | |
| 614 size_t decrypted_length = 0; | |
| 615 if (!DecryptPayload(encrypted_reader, header, packet, decrypted_buffer, | |
| 616 buffer_length, &decrypted_length)) { | |
| 617 set_detailed_error("Unable to decrypt payload."); | |
| 618 return RaiseError(QUIC_DECRYPTION_FAILURE); | |
| 619 } | |
| 620 | |
| 621 QuicDataReader reader(decrypted_buffer, decrypted_length); | |
| 622 if (quic_version_ <= QUIC_VERSION_33) { | |
| 623 if (!ProcessAuthenticatedHeader(&reader, &header)) { | |
| 624 DLOG(WARNING) << "Unable to process packet header. Stopping parsing."; | |
| 625 return false; | |
| 626 } | |
| 627 } | |
| 628 | |
| 629 // Set the last packet number after we have decrypted the packet | |
| 630 // so we are confident is not attacker controlled. | |
| 631 SetLastPacketNumber(header); | |
| 632 | |
| 633 if (!visitor_->OnPacketHeader(header)) { | |
| 634 // The visitor suppresses further processing of the packet. | |
| 635 return true; | |
| 636 } | |
| 637 | |
| 638 if (packet.length() > kMaxPacketSize) { | |
| 639 // If the packet has gotten this far, it should not be too large. | |
| 640 QUIC_BUG << "Packet too large:" << packet.length(); | |
| 641 return RaiseError(QUIC_PACKET_TOO_LARGE); | |
| 642 } | |
| 643 | |
| 644 DCHECK(!header.fec_flag); | |
| 645 // Handle the payload. | |
| 646 if (!ProcessFrameData(&reader, header)) { | |
| 647 DCHECK_NE(QUIC_NO_ERROR, error_); // ProcessFrameData sets the error. | |
| 648 DLOG(WARNING) << "Unable to process frame data."; | |
| 649 return false; | |
| 650 } | |
| 651 | |
| 652 visitor_->OnPacketComplete(); | |
| 653 return true; | |
| 654 } | |
| 655 | |
| 656 bool QuicFramer::ProcessPublicResetPacket( | |
| 657 QuicDataReader* reader, | |
| 658 const QuicPacketPublicHeader& public_header) { | |
| 659 QuicPublicResetPacket packet(public_header); | |
| 660 | |
| 661 std::unique_ptr<CryptoHandshakeMessage> reset( | |
| 662 CryptoFramer::ParseMessage(reader->ReadRemainingPayload())); | |
| 663 if (!reset.get()) { | |
| 664 set_detailed_error("Unable to read reset message."); | |
| 665 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); | |
| 666 } | |
| 667 if (reset->tag() != kPRST) { | |
| 668 set_detailed_error("Incorrect message tag."); | |
| 669 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); | |
| 670 } | |
| 671 | |
| 672 if (reset->GetUint64(kRNON, &packet.nonce_proof) != QUIC_NO_ERROR) { | |
| 673 set_detailed_error("Unable to read nonce proof."); | |
| 674 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); | |
| 675 } | |
| 676 // TODO(satyamshekhar): validate nonce to protect against DoS. | |
| 677 | |
| 678 StringPiece address; | |
| 679 if (reset->GetStringPiece(kCADR, &address)) { | |
| 680 QuicSocketAddressCoder address_coder; | |
| 681 if (address_coder.Decode(address.data(), address.length())) { | |
| 682 packet.client_address = | |
| 683 IPEndPoint(address_coder.ip(), address_coder.port()); | |
| 684 } | |
| 685 } | |
| 686 | |
| 687 visitor_->OnPublicResetPacket(packet); | |
| 688 return true; | |
| 689 } | |
| 690 | |
| 691 bool QuicFramer::AppendPacketHeader(const QuicPacketHeader& header, | |
| 692 QuicDataWriter* writer) { | |
| 693 DVLOG(1) << "Appending header: " << header; | |
| 694 uint8_t public_flags = 0; | |
| 695 if (header.public_header.reset_flag) { | |
| 696 public_flags |= PACKET_PUBLIC_FLAGS_RST; | |
| 697 } | |
| 698 if (header.public_header.version_flag) { | |
| 699 public_flags |= PACKET_PUBLIC_FLAGS_VERSION; | |
| 700 } | |
| 701 if (header.public_header.multipath_flag) { | |
| 702 public_flags |= PACKET_PUBLIC_FLAGS_MULTIPATH; | |
| 703 } | |
| 704 | |
| 705 public_flags |= | |
| 706 GetSequenceNumberFlags(header.public_header.packet_number_length) | |
| 707 << kPublicHeaderSequenceNumberShift; | |
| 708 | |
| 709 if (header.public_header.nonce != nullptr) { | |
| 710 DCHECK_EQ(Perspective::IS_SERVER, perspective_); | |
| 711 public_flags |= PACKET_PUBLIC_FLAGS_NONCE; | |
| 712 } | |
| 713 | |
| 714 switch (header.public_header.connection_id_length) { | |
| 715 case PACKET_0BYTE_CONNECTION_ID: | |
| 716 if (!writer->WriteUInt8(public_flags | | |
| 717 PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID)) { | |
| 718 return false; | |
| 719 } | |
| 720 break; | |
| 721 case PACKET_8BYTE_CONNECTION_ID: | |
| 722 if (quic_version_ > QUIC_VERSION_32) { | |
| 723 public_flags |= PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID; | |
| 724 if (perspective_ == Perspective::IS_CLIENT) { | |
| 725 // TODO(rch): Fix this when v33 flags are supported by middle boxes. | |
| 726 public_flags |= PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD; | |
| 727 } | |
| 728 | |
| 729 } else { | |
| 730 public_flags |= PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD; | |
| 731 } | |
| 732 if (!writer->WriteUInt8(public_flags) || | |
| 733 !writer->WriteUInt64(header.public_header.connection_id)) { | |
| 734 return false; | |
| 735 } | |
| 736 break; | |
| 737 } | |
| 738 last_serialized_connection_id_ = header.public_header.connection_id; | |
| 739 | |
| 740 if (header.public_header.version_flag) { | |
| 741 DCHECK_EQ(Perspective::IS_CLIENT, perspective_); | |
| 742 QuicTag tag = QuicVersionToQuicTag(quic_version_); | |
| 743 writer->WriteUInt32(tag); | |
| 744 DVLOG(1) << "version = " << quic_version_ << ", tag = '" | |
| 745 << QuicUtils::TagToString(tag) << "'"; | |
| 746 } | |
| 747 | |
| 748 if (header.public_header.multipath_flag && | |
| 749 !writer->WriteUInt8(header.path_id)) { | |
| 750 return false; | |
| 751 } | |
| 752 | |
| 753 if (header.public_header.nonce != nullptr && | |
| 754 !writer->WriteBytes(header.public_header.nonce, | |
| 755 kDiversificationNonceSize)) { | |
| 756 return false; | |
| 757 } | |
| 758 | |
| 759 if (!AppendPacketSequenceNumber(header.public_header.packet_number_length, | |
| 760 header.packet_number, writer)) { | |
| 761 return false; | |
| 762 } | |
| 763 if (quic_version_ > QUIC_VERSION_33) { | |
| 764 return true; | |
| 765 } | |
| 766 | |
| 767 uint8_t private_flags = 0; | |
| 768 if (header.entropy_flag) { | |
| 769 private_flags |= PACKET_PRIVATE_FLAGS_ENTROPY; | |
| 770 } | |
| 771 if (!writer->WriteUInt8(private_flags)) { | |
| 772 return false; | |
| 773 } | |
| 774 | |
| 775 return true; | |
| 776 } | |
| 777 | |
| 778 const QuicTime::Delta QuicFramer::CalculateTimestampFromWire( | |
| 779 uint32_t time_delta_us) { | |
| 780 // The new time_delta might have wrapped to the next epoch, or it | |
| 781 // might have reverse wrapped to the previous epoch, or it might | |
| 782 // remain in the same epoch. Select the time closest to the previous | |
| 783 // time. | |
| 784 // | |
| 785 // epoch_delta is the delta between epochs. A delta is 4 bytes of | |
| 786 // microseconds. | |
| 787 const uint64_t epoch_delta = UINT64_C(1) << 32; | |
| 788 uint64_t epoch = last_timestamp_.ToMicroseconds() & ~(epoch_delta - 1); | |
| 789 // Wrapping is safe here because a wrapped value will not be ClosestTo below. | |
| 790 uint64_t prev_epoch = epoch - epoch_delta; | |
| 791 uint64_t next_epoch = epoch + epoch_delta; | |
| 792 | |
| 793 uint64_t time = ClosestTo( | |
| 794 last_timestamp_.ToMicroseconds(), epoch + time_delta_us, | |
| 795 ClosestTo(last_timestamp_.ToMicroseconds(), prev_epoch + time_delta_us, | |
| 796 next_epoch + time_delta_us)); | |
| 797 | |
| 798 return QuicTime::Delta::FromMicroseconds(time); | |
| 799 } | |
| 800 | |
| 801 bool QuicFramer::IsValidPath(QuicPathId path_id, | |
| 802 QuicPacketNumber* last_packet_number) { | |
| 803 if (ContainsKey(closed_paths_, path_id)) { | |
| 804 // Path is closed. | |
| 805 return false; | |
| 806 } | |
| 807 | |
| 808 if (path_id == last_path_id_) { | |
| 809 *last_packet_number = last_packet_number_; | |
| 810 return true; | |
| 811 } | |
| 812 | |
| 813 if (ContainsKey(last_packet_numbers_, path_id)) { | |
| 814 *last_packet_number = last_packet_numbers_[path_id]; | |
| 815 } else { | |
| 816 *last_packet_number = 0; | |
| 817 } | |
| 818 | |
| 819 return true; | |
| 820 } | |
| 821 | |
| 822 void QuicFramer::SetLastPacketNumber(const QuicPacketHeader& header) { | |
| 823 if (header.public_header.multipath_flag && header.path_id != last_path_id_) { | |
| 824 if (last_path_id_ != kInvalidPathId) { | |
| 825 // Save current last packet number before changing path. | |
| 826 last_packet_numbers_[last_path_id_] = last_packet_number_; | |
| 827 } | |
| 828 // Change path. | |
| 829 last_path_id_ = header.path_id; | |
| 830 } | |
| 831 last_packet_number_ = header.packet_number; | |
| 832 } | |
| 833 | |
| 834 void QuicFramer::OnPathClosed(QuicPathId path_id) { | |
| 835 closed_paths_.insert(path_id); | |
| 836 last_packet_numbers_.erase(path_id); | |
| 837 } | |
| 838 | |
| 839 QuicPacketNumber QuicFramer::CalculatePacketNumberFromWire( | |
| 840 QuicPacketNumberLength packet_number_length, | |
| 841 QuicPacketNumber last_packet_number, | |
| 842 QuicPacketNumber packet_number) const { | |
| 843 // The new packet number might have wrapped to the next epoch, or | |
| 844 // it might have reverse wrapped to the previous epoch, or it might | |
| 845 // remain in the same epoch. Select the packet number closest to the | |
| 846 // next expected packet number, the previous packet number plus 1. | |
| 847 | |
| 848 // epoch_delta is the delta between epochs the packet number was serialized | |
| 849 // with, so the correct value is likely the same epoch as the last sequence | |
| 850 // number or an adjacent epoch. | |
| 851 const QuicPacketNumber epoch_delta = UINT64_C(1) | |
| 852 << (8 * packet_number_length); | |
| 853 QuicPacketNumber next_packet_number = last_packet_number + 1; | |
| 854 QuicPacketNumber epoch = last_packet_number & ~(epoch_delta - 1); | |
| 855 QuicPacketNumber prev_epoch = epoch - epoch_delta; | |
| 856 QuicPacketNumber next_epoch = epoch + epoch_delta; | |
| 857 | |
| 858 return ClosestTo(next_packet_number, epoch + packet_number, | |
| 859 ClosestTo(next_packet_number, prev_epoch + packet_number, | |
| 860 next_epoch + packet_number)); | |
| 861 } | |
| 862 | |
| 863 bool QuicFramer::ProcessPublicHeader(QuicDataReader* reader, | |
| 864 QuicPacketPublicHeader* public_header) { | |
| 865 uint8_t public_flags; | |
| 866 if (!reader->ReadBytes(&public_flags, 1)) { | |
| 867 set_detailed_error("Unable to read public flags."); | |
| 868 return false; | |
| 869 } | |
| 870 | |
| 871 public_header->multipath_flag = | |
| 872 (public_flags & PACKET_PUBLIC_FLAGS_MULTIPATH) != 0; | |
| 873 public_header->reset_flag = (public_flags & PACKET_PUBLIC_FLAGS_RST) != 0; | |
| 874 public_header->version_flag = | |
| 875 (public_flags & PACKET_PUBLIC_FLAGS_VERSION) != 0; | |
| 876 | |
| 877 if (validate_flags_ && !public_header->version_flag && | |
| 878 public_flags > PACKET_PUBLIC_FLAGS_MAX) { | |
| 879 set_detailed_error("Illegal public flags value."); | |
| 880 return false; | |
| 881 } | |
| 882 | |
| 883 if (public_header->reset_flag && public_header->version_flag) { | |
| 884 set_detailed_error("Got version flag in reset packet"); | |
| 885 return false; | |
| 886 } | |
| 887 | |
| 888 switch (public_flags & PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID) { | |
| 889 case PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID: | |
| 890 if (!reader->ReadUInt64(&public_header->connection_id)) { | |
| 891 set_detailed_error("Unable to read ConnectionId."); | |
| 892 return false; | |
| 893 } | |
| 894 public_header->connection_id_length = PACKET_8BYTE_CONNECTION_ID; | |
| 895 break; | |
| 896 case PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID: | |
| 897 public_header->connection_id_length = PACKET_0BYTE_CONNECTION_ID; | |
| 898 public_header->connection_id = last_serialized_connection_id_; | |
| 899 break; | |
| 900 } | |
| 901 | |
| 902 public_header->packet_number_length = ReadSequenceNumberLength( | |
| 903 public_flags >> kPublicHeaderSequenceNumberShift); | |
| 904 | |
| 905 // Read the version only if the packet is from the client. | |
| 906 // version flag from the server means version negotiation packet. | |
| 907 if (public_header->version_flag && perspective_ == Perspective::IS_SERVER) { | |
| 908 QuicTag version_tag; | |
| 909 if (!reader->ReadUInt32(&version_tag)) { | |
| 910 set_detailed_error("Unable to read protocol version."); | |
| 911 return false; | |
| 912 } | |
| 913 | |
| 914 // If the version from the new packet is the same as the version of this | |
| 915 // framer, then the public flags should be set to something we understand. | |
| 916 // If not, this raises an error. | |
| 917 last_version_tag_ = version_tag; | |
| 918 QuicVersion version = QuicTagToQuicVersion(version_tag); | |
| 919 if (version == quic_version_ && public_flags > PACKET_PUBLIC_FLAGS_MAX) { | |
| 920 set_detailed_error("Illegal public flags value."); | |
| 921 return false; | |
| 922 } | |
| 923 public_header->versions.push_back(version); | |
| 924 } | |
| 925 | |
| 926 // A nonce should only be present in packets from the server to the client, | |
| 927 // which are neither version negotiation nor public reset packets | |
| 928 // and only for versions after QUIC_VERSION_32. Earlier versions will | |
| 929 // set this bit when indicating an 8-byte connection ID, which should | |
| 930 // not be interpreted as indicating a nonce is present. | |
| 931 if (quic_version_ > QUIC_VERSION_32 && | |
| 932 public_flags & PACKET_PUBLIC_FLAGS_NONCE && | |
| 933 !(public_flags & PACKET_PUBLIC_FLAGS_VERSION) && | |
| 934 !(public_flags & PACKET_PUBLIC_FLAGS_RST) && | |
| 935 // The nonce flag from a client is ignored and is assumed to be an older | |
| 936 // client indicating an eight-byte connection ID. | |
| 937 perspective_ == Perspective::IS_CLIENT) { | |
| 938 if (!reader->ReadBytes(reinterpret_cast<uint8_t*>(last_nonce_), | |
| 939 sizeof(last_nonce_))) { | |
| 940 set_detailed_error("Unable to read nonce."); | |
| 941 return false; | |
| 942 } | |
| 943 public_header->nonce = &last_nonce_; | |
| 944 } else { | |
| 945 public_header->nonce = nullptr; | |
| 946 } | |
| 947 | |
| 948 return true; | |
| 949 } | |
| 950 | |
| 951 // static | |
| 952 QuicPacketNumberLength QuicFramer::GetMinSequenceNumberLength( | |
| 953 QuicPacketNumber packet_number) { | |
| 954 if (packet_number < 1 << (PACKET_1BYTE_PACKET_NUMBER * 8)) { | |
| 955 return PACKET_1BYTE_PACKET_NUMBER; | |
| 956 } else if (packet_number < 1 << (PACKET_2BYTE_PACKET_NUMBER * 8)) { | |
| 957 return PACKET_2BYTE_PACKET_NUMBER; | |
| 958 } else if (packet_number < UINT64_C(1) << (PACKET_4BYTE_PACKET_NUMBER * 8)) { | |
| 959 return PACKET_4BYTE_PACKET_NUMBER; | |
| 960 } else { | |
| 961 return PACKET_6BYTE_PACKET_NUMBER; | |
| 962 } | |
| 963 } | |
| 964 | |
| 965 // static | |
| 966 uint8_t QuicFramer::GetSequenceNumberFlags( | |
| 967 QuicPacketNumberLength packet_number_length) { | |
| 968 switch (packet_number_length) { | |
| 969 case PACKET_1BYTE_PACKET_NUMBER: | |
| 970 return PACKET_FLAGS_1BYTE_PACKET; | |
| 971 case PACKET_2BYTE_PACKET_NUMBER: | |
| 972 return PACKET_FLAGS_2BYTE_PACKET; | |
| 973 case PACKET_4BYTE_PACKET_NUMBER: | |
| 974 return PACKET_FLAGS_4BYTE_PACKET; | |
| 975 case PACKET_6BYTE_PACKET_NUMBER: | |
| 976 return PACKET_FLAGS_6BYTE_PACKET; | |
| 977 default: | |
| 978 QUIC_BUG << "Unreachable case statement."; | |
| 979 return PACKET_FLAGS_6BYTE_PACKET; | |
| 980 } | |
| 981 } | |
| 982 | |
| 983 // static | |
| 984 QuicFramer::AckFrameInfo QuicFramer::GetAckFrameInfo( | |
| 985 const QuicAckFrame& frame) { | |
| 986 AckFrameInfo ack_info; | |
| 987 if (frame.packets.Empty()) { | |
| 988 return ack_info; | |
| 989 } | |
| 990 DCHECK_GE(frame.largest_observed, frame.packets.Max()); | |
| 991 if (FLAGS_quic_use_packet_number_queue_intervals) { | |
| 992 QuicPacketNumber last_largest_missing = 0; | |
| 993 for (auto itr = frame.packets.begin_intervals(); | |
| 994 itr != frame.packets.end_intervals(); ++itr) { | |
| 995 const Interval<QuicPacketNumber>& interval = *itr; | |
| 996 for (QuicPacketNumber interval_start = interval.min(); | |
| 997 interval_start < interval.max(); | |
| 998 interval_start += (1ull + numeric_limits<uint8_t>::max())) { | |
| 999 uint8_t cur_range_length = | |
| 1000 interval.max() - interval_start > numeric_limits<uint8_t>::max() | |
| 1001 ? numeric_limits<uint8_t>::max() | |
| 1002 : (interval.max() - interval_start) - 1; | |
| 1003 ack_info.nack_ranges[interval_start] = cur_range_length; | |
| 1004 } | |
| 1005 ack_info.max_delta = max(ack_info.max_delta, | |
| 1006 last_largest_missing == 0 | |
| 1007 ? QuicPacketNumber{0} | |
| 1008 : (interval.min() - last_largest_missing)); | |
| 1009 last_largest_missing = interval.max() - 1; | |
| 1010 } | |
| 1011 // Include the range to the largest observed. | |
| 1012 ack_info.max_delta = | |
| 1013 max(ack_info.max_delta, frame.largest_observed - last_largest_missing); | |
| 1014 } else { | |
| 1015 size_t cur_range_length = 0; | |
| 1016 PacketNumberQueue::const_iterator iter = frame.packets.begin(); | |
| 1017 QuicPacketNumber last_missing = *iter; | |
| 1018 ++iter; | |
| 1019 for (; iter != frame.packets.end(); ++iter) { | |
| 1020 if (cur_range_length < numeric_limits<uint8_t>::max() && | |
| 1021 *iter == (last_missing + 1)) { | |
| 1022 ++cur_range_length; | |
| 1023 } else { | |
| 1024 ack_info.nack_ranges[last_missing - cur_range_length] = | |
| 1025 static_cast<uint8_t>(cur_range_length); | |
| 1026 cur_range_length = 0; | |
| 1027 } | |
| 1028 ack_info.max_delta = max(ack_info.max_delta, *iter - last_missing); | |
| 1029 last_missing = *iter; | |
| 1030 } | |
| 1031 // Include the last nack range. | |
| 1032 ack_info.nack_ranges[last_missing - cur_range_length] = | |
| 1033 static_cast<uint8_t>(cur_range_length); | |
| 1034 // Include the range to the largest observed. | |
| 1035 ack_info.max_delta = | |
| 1036 max(ack_info.max_delta, frame.largest_observed - last_missing); | |
| 1037 } | |
| 1038 return ack_info; | |
| 1039 } | |
| 1040 | |
| 1041 // static | |
| 1042 QuicFramer::NewAckFrameInfo QuicFramer::GetNewAckFrameInfo( | |
| 1043 const QuicAckFrame& frame, | |
| 1044 bool construct_blocks) { | |
| 1045 NewAckFrameInfo new_ack_info; | |
| 1046 if (frame.packets.Empty()) { | |
| 1047 return new_ack_info; | |
| 1048 } | |
| 1049 if (!construct_blocks) { | |
| 1050 // The first block is the last interval. It isn't encoded with the | |
| 1051 // gap-length encoding, so skip it. | |
| 1052 new_ack_info.first_block_length = frame.packets.LastIntervalLength(); | |
| 1053 auto itr = frame.packets.rbegin_intervals(); | |
| 1054 QuicPacketNumber previous_start = itr->min(); | |
| 1055 new_ack_info.max_block_length = itr->Length(); | |
| 1056 ++itr; | |
| 1057 | |
| 1058 // Don't do any more work after getting information for 256 ACK blocks; any | |
| 1059 // more can't be encoded anyway. | |
| 1060 for (; itr != frame.packets.rend_intervals() && | |
| 1061 new_ack_info.num_ack_blocks < numeric_limits<uint8_t>::max(); | |
| 1062 previous_start = itr->min(), ++itr) { | |
| 1063 const auto& interval = *itr; | |
| 1064 const QuicPacketNumber total_gap = previous_start - interval.max(); | |
| 1065 new_ack_info.num_ack_blocks += | |
| 1066 (total_gap + numeric_limits<uint8_t>::max() - 1) / | |
| 1067 numeric_limits<uint8_t>::max(); | |
| 1068 new_ack_info.max_block_length = | |
| 1069 max(new_ack_info.max_block_length, interval.Length()); | |
| 1070 } | |
| 1071 } else { | |
| 1072 QuicPacketNumber cur_range_length = 1; | |
| 1073 PacketNumberQueue::const_iterator iter = frame.packets.begin(); | |
| 1074 QuicPacketNumber last_received = *iter; | |
| 1075 ++iter; | |
| 1076 for (; iter != frame.packets.end(); ++iter) { | |
| 1077 if (*iter == (last_received + 1)) { | |
| 1078 ++cur_range_length; | |
| 1079 } else { | |
| 1080 size_t total_gap = *iter - last_received - 1; | |
| 1081 size_t num_blocks = static_cast<size_t>(ceil( | |
| 1082 static_cast<double>(total_gap) / numeric_limits<uint8_t>::max())); | |
| 1083 uint8_t last_gap = static_cast<uint8_t>( | |
| 1084 total_gap - (num_blocks - 1) * numeric_limits<uint8_t>::max()); | |
| 1085 for (size_t i = 0; i < num_blocks; ++i) { | |
| 1086 if (i == 0) { | |
| 1087 new_ack_info.ack_blocks.push_back( | |
| 1088 AckBlock(last_gap, cur_range_length)); | |
| 1089 } else { | |
| 1090 // Add an ack block of length 0 because there are more than 255 | |
| 1091 // missing packets in a row. | |
| 1092 new_ack_info.ack_blocks.push_back( | |
| 1093 AckBlock(numeric_limits<uint8_t>::max(), 0)); | |
| 1094 } | |
| 1095 } | |
| 1096 new_ack_info.max_block_length = | |
| 1097 max(new_ack_info.max_block_length, cur_range_length); | |
| 1098 cur_range_length = 1; | |
| 1099 } | |
| 1100 last_received = *iter; | |
| 1101 } | |
| 1102 new_ack_info.first_block_length = cur_range_length; | |
| 1103 new_ack_info.max_block_length = | |
| 1104 max(new_ack_info.max_block_length, new_ack_info.first_block_length); | |
| 1105 new_ack_info.num_ack_blocks = new_ack_info.ack_blocks.size(); | |
| 1106 } | |
| 1107 return new_ack_info; | |
| 1108 } | |
| 1109 | |
| 1110 bool QuicFramer::ProcessUnauthenticatedHeader(QuicDataReader* encrypted_reader, | |
| 1111 QuicPacketHeader* header) { | |
| 1112 header->path_id = kDefaultPathId; | |
| 1113 if (header->public_header.multipath_flag && | |
| 1114 !ProcessPathId(encrypted_reader, &header->path_id)) { | |
| 1115 set_detailed_error("Unable to read path id."); | |
| 1116 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1117 } | |
| 1118 | |
| 1119 QuicPacketNumber last_packet_number = last_packet_number_; | |
| 1120 if (header->public_header.multipath_flag && | |
| 1121 !IsValidPath(header->path_id, &last_packet_number)) { | |
| 1122 // Stop processing because path is closed. | |
| 1123 return false; | |
| 1124 } | |
| 1125 | |
| 1126 if (!ProcessPacketSequenceNumber( | |
| 1127 encrypted_reader, header->public_header.packet_number_length, | |
| 1128 last_packet_number, &header->packet_number)) { | |
| 1129 set_detailed_error("Unable to read packet number."); | |
| 1130 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1131 } | |
| 1132 | |
| 1133 if (header->packet_number == 0u) { | |
| 1134 set_detailed_error("packet numbers cannot be 0."); | |
| 1135 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1136 } | |
| 1137 | |
| 1138 if (!visitor_->OnUnauthenticatedHeader(*header)) { | |
| 1139 return false; | |
| 1140 } | |
| 1141 return true; | |
| 1142 } | |
| 1143 | |
| 1144 bool QuicFramer::ProcessAuthenticatedHeader(QuicDataReader* reader, | |
| 1145 QuicPacketHeader* header) { | |
| 1146 uint8_t private_flags; | |
| 1147 if (!reader->ReadBytes(&private_flags, 1)) { | |
| 1148 set_detailed_error("Unable to read private flags."); | |
| 1149 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1150 } | |
| 1151 | |
| 1152 if (quic_version_ > QUIC_VERSION_31) { | |
| 1153 if (private_flags > PACKET_PRIVATE_FLAGS_MAX_VERSION_32) { | |
| 1154 set_detailed_error("Illegal private flags value."); | |
| 1155 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1156 } | |
| 1157 } else { | |
| 1158 if (private_flags > PACKET_PRIVATE_FLAGS_MAX) { | |
| 1159 set_detailed_error("Illegal private flags value."); | |
| 1160 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1161 } | |
| 1162 } | |
| 1163 | |
| 1164 header->entropy_flag = (private_flags & PACKET_PRIVATE_FLAGS_ENTROPY) != 0; | |
| 1165 header->fec_flag = (private_flags & PACKET_PRIVATE_FLAGS_FEC) != 0; | |
| 1166 | |
| 1167 if ((private_flags & PACKET_PRIVATE_FLAGS_FEC_GROUP) != 0) { | |
| 1168 uint8_t first_fec_protected_packet_offset; | |
| 1169 if (!reader->ReadBytes(&first_fec_protected_packet_offset, 1)) { | |
| 1170 set_detailed_error("Unable to read first fec protected packet offset."); | |
| 1171 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1172 } | |
| 1173 if (first_fec_protected_packet_offset >= header->packet_number) { | |
| 1174 set_detailed_error( | |
| 1175 "First fec protected packet offset must be less " | |
| 1176 "than the packet number."); | |
| 1177 return RaiseError(QUIC_INVALID_PACKET_HEADER); | |
| 1178 } | |
| 1179 } | |
| 1180 | |
| 1181 header->entropy_hash = GetPacketEntropyHash(*header); | |
| 1182 return true; | |
| 1183 } | |
| 1184 | |
| 1185 bool QuicFramer::ProcessPathId(QuicDataReader* reader, QuicPathId* path_id) { | |
| 1186 if (!reader->ReadBytes(path_id, 1)) { | |
| 1187 return false; | |
| 1188 } | |
| 1189 | |
| 1190 return true; | |
| 1191 } | |
| 1192 | |
| 1193 bool QuicFramer::ProcessPacketSequenceNumber( | |
| 1194 QuicDataReader* reader, | |
| 1195 QuicPacketNumberLength packet_number_length, | |
| 1196 QuicPacketNumber last_packet_number, | |
| 1197 QuicPacketNumber* packet_number) { | |
| 1198 QuicPacketNumber wire_packet_number = 0u; | |
| 1199 if (!reader->ReadBytes(&wire_packet_number, packet_number_length)) { | |
| 1200 return false; | |
| 1201 } | |
| 1202 | |
| 1203 // TODO(ianswett): Explore the usefulness of trying multiple packet numbers | |
| 1204 // in case the first guess is incorrect. | |
| 1205 *packet_number = CalculatePacketNumberFromWire( | |
| 1206 packet_number_length, last_packet_number, wire_packet_number); | |
| 1207 return true; | |
| 1208 } | |
| 1209 | |
| 1210 bool QuicFramer::ProcessFrameData(QuicDataReader* reader, | |
| 1211 const QuicPacketHeader& header) { | |
| 1212 if (reader->IsDoneReading()) { | |
| 1213 set_detailed_error("Packet has no frames."); | |
| 1214 return RaiseError(QUIC_MISSING_PAYLOAD); | |
| 1215 } | |
| 1216 while (!reader->IsDoneReading()) { | |
| 1217 uint8_t frame_type; | |
| 1218 if (!reader->ReadBytes(&frame_type, 1)) { | |
| 1219 set_detailed_error("Unable to read frame type."); | |
| 1220 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
| 1221 } | |
| 1222 | |
| 1223 if (frame_type & kQuicFrameTypeSpecialMask) { | |
| 1224 // Stream Frame | |
| 1225 if (frame_type & kQuicFrameTypeStreamMask) { | |
| 1226 QuicStreamFrame frame; | |
| 1227 if (!ProcessStreamFrame(reader, frame_type, &frame)) { | |
| 1228 return RaiseError(QUIC_INVALID_STREAM_DATA); | |
| 1229 } | |
| 1230 if (!visitor_->OnStreamFrame(frame)) { | |
| 1231 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1232 // Returning true since there was no parsing error. | |
| 1233 return true; | |
| 1234 } | |
| 1235 continue; | |
| 1236 } | |
| 1237 | |
| 1238 // Ack Frame | |
| 1239 if (frame_type & kQuicFrameTypeAckMask) { | |
| 1240 QuicAckFrame frame; | |
| 1241 if (quic_version_ <= QUIC_VERSION_33) { | |
| 1242 if (!ProcessAckFrame(reader, frame_type, &frame)) { | |
| 1243 return RaiseError(QUIC_INVALID_ACK_DATA); | |
| 1244 } | |
| 1245 } else { | |
| 1246 if (!ProcessNewAckFrame(reader, frame_type, &frame)) { | |
| 1247 return RaiseError(QUIC_INVALID_ACK_DATA); | |
| 1248 } | |
| 1249 } | |
| 1250 if (!visitor_->OnAckFrame(frame)) { | |
| 1251 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1252 // Returning true since there was no parsing error. | |
| 1253 return true; | |
| 1254 } | |
| 1255 continue; | |
| 1256 } | |
| 1257 | |
| 1258 // This was a special frame type that did not match any | |
| 1259 // of the known ones. Error. | |
| 1260 set_detailed_error("Illegal frame type."); | |
| 1261 DLOG(WARNING) << "Illegal frame type: " << static_cast<int>(frame_type); | |
| 1262 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
| 1263 } | |
| 1264 | |
| 1265 switch (frame_type) { | |
| 1266 case PADDING_FRAME: { | |
| 1267 QuicPaddingFrame frame(reader->BytesRemaining()); | |
| 1268 if (!visitor_->OnPaddingFrame(frame)) { | |
| 1269 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1270 } | |
| 1271 // We're done with the packet. | |
| 1272 return true; | |
| 1273 } | |
| 1274 | |
| 1275 case RST_STREAM_FRAME: { | |
| 1276 QuicRstStreamFrame frame; | |
| 1277 if (!ProcessRstStreamFrame(reader, &frame)) { | |
| 1278 return RaiseError(QUIC_INVALID_RST_STREAM_DATA); | |
| 1279 } | |
| 1280 if (!visitor_->OnRstStreamFrame(frame)) { | |
| 1281 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1282 // Returning true since there was no parsing error. | |
| 1283 return true; | |
| 1284 } | |
| 1285 continue; | |
| 1286 } | |
| 1287 | |
| 1288 case CONNECTION_CLOSE_FRAME: { | |
| 1289 QuicConnectionCloseFrame frame; | |
| 1290 if (!ProcessConnectionCloseFrame(reader, &frame)) { | |
| 1291 return RaiseError(QUIC_INVALID_CONNECTION_CLOSE_DATA); | |
| 1292 } | |
| 1293 | |
| 1294 if (!visitor_->OnConnectionCloseFrame(frame)) { | |
| 1295 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1296 // Returning true since there was no parsing error. | |
| 1297 return true; | |
| 1298 } | |
| 1299 continue; | |
| 1300 } | |
| 1301 | |
| 1302 case GOAWAY_FRAME: { | |
| 1303 QuicGoAwayFrame goaway_frame; | |
| 1304 if (!ProcessGoAwayFrame(reader, &goaway_frame)) { | |
| 1305 return RaiseError(QUIC_INVALID_GOAWAY_DATA); | |
| 1306 } | |
| 1307 if (!visitor_->OnGoAwayFrame(goaway_frame)) { | |
| 1308 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1309 // Returning true since there was no parsing error. | |
| 1310 return true; | |
| 1311 } | |
| 1312 continue; | |
| 1313 } | |
| 1314 | |
| 1315 case WINDOW_UPDATE_FRAME: { | |
| 1316 QuicWindowUpdateFrame window_update_frame; | |
| 1317 if (!ProcessWindowUpdateFrame(reader, &window_update_frame)) { | |
| 1318 return RaiseError(QUIC_INVALID_WINDOW_UPDATE_DATA); | |
| 1319 } | |
| 1320 if (!visitor_->OnWindowUpdateFrame(window_update_frame)) { | |
| 1321 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1322 // Returning true since there was no parsing error. | |
| 1323 return true; | |
| 1324 } | |
| 1325 continue; | |
| 1326 } | |
| 1327 | |
| 1328 case BLOCKED_FRAME: { | |
| 1329 QuicBlockedFrame blocked_frame; | |
| 1330 if (!ProcessBlockedFrame(reader, &blocked_frame)) { | |
| 1331 return RaiseError(QUIC_INVALID_BLOCKED_DATA); | |
| 1332 } | |
| 1333 if (!visitor_->OnBlockedFrame(blocked_frame)) { | |
| 1334 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1335 // Returning true since there was no parsing error. | |
| 1336 return true; | |
| 1337 } | |
| 1338 continue; | |
| 1339 } | |
| 1340 | |
| 1341 case STOP_WAITING_FRAME: { | |
| 1342 QuicStopWaitingFrame stop_waiting_frame; | |
| 1343 if (!ProcessStopWaitingFrame(reader, header, &stop_waiting_frame)) { | |
| 1344 return RaiseError(QUIC_INVALID_STOP_WAITING_DATA); | |
| 1345 } | |
| 1346 if (!visitor_->OnStopWaitingFrame(stop_waiting_frame)) { | |
| 1347 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1348 // Returning true since there was no parsing error. | |
| 1349 return true; | |
| 1350 } | |
| 1351 continue; | |
| 1352 } | |
| 1353 case PING_FRAME: { | |
| 1354 // Ping has no payload. | |
| 1355 QuicPingFrame ping_frame; | |
| 1356 if (!visitor_->OnPingFrame(ping_frame)) { | |
| 1357 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1358 // Returning true since there was no parsing error. | |
| 1359 return true; | |
| 1360 } | |
| 1361 continue; | |
| 1362 } | |
| 1363 case PATH_CLOSE_FRAME: { | |
| 1364 QuicPathCloseFrame path_close_frame; | |
| 1365 if (!ProcessPathCloseFrame(reader, &path_close_frame)) { | |
| 1366 return RaiseError(QUIC_INVALID_PATH_CLOSE_DATA); | |
| 1367 } | |
| 1368 if (!visitor_->OnPathCloseFrame(path_close_frame)) { | |
| 1369 DVLOG(1) << "Visitor asked to stop further processing."; | |
| 1370 // Returning true since there was no parsing error. | |
| 1371 return true; | |
| 1372 } | |
| 1373 continue; | |
| 1374 } | |
| 1375 | |
| 1376 default: | |
| 1377 set_detailed_error("Illegal frame type."); | |
| 1378 DLOG(WARNING) << "Illegal frame type: " << static_cast<int>(frame_type); | |
| 1379 return RaiseError(QUIC_INVALID_FRAME_DATA); | |
| 1380 } | |
| 1381 } | |
| 1382 | |
| 1383 return true; | |
| 1384 } | |
| 1385 | |
| 1386 bool QuicFramer::ProcessStreamFrame(QuicDataReader* reader, | |
| 1387 uint8_t frame_type, | |
| 1388 QuicStreamFrame* frame) { | |
| 1389 uint8_t stream_flags = frame_type; | |
| 1390 | |
| 1391 stream_flags &= ~kQuicFrameTypeStreamMask; | |
| 1392 | |
| 1393 // Read from right to left: StreamID, Offset, Data Length, Fin. | |
| 1394 const uint8_t stream_id_length = (stream_flags & kQuicStreamIDLengthMask) + 1; | |
| 1395 stream_flags >>= kQuicStreamIdShift; | |
| 1396 | |
| 1397 uint8_t offset_length = (stream_flags & kQuicStreamOffsetMask); | |
| 1398 // There is no encoding for 1 byte, only 0 and 2 through 8. | |
| 1399 if (offset_length > 0) { | |
| 1400 offset_length += 1; | |
| 1401 } | |
| 1402 stream_flags >>= kQuicStreamOffsetShift; | |
| 1403 | |
| 1404 bool has_data_length = | |
| 1405 (stream_flags & kQuicStreamDataLengthMask) == kQuicStreamDataLengthMask; | |
| 1406 stream_flags >>= kQuicStreamDataLengthShift; | |
| 1407 | |
| 1408 frame->fin = (stream_flags & kQuicStreamFinMask) == kQuicStreamFinShift; | |
| 1409 | |
| 1410 frame->stream_id = 0; | |
| 1411 if (!reader->ReadBytes(&frame->stream_id, stream_id_length)) { | |
| 1412 set_detailed_error("Unable to read stream_id."); | |
| 1413 return false; | |
| 1414 } | |
| 1415 | |
| 1416 frame->offset = 0; | |
| 1417 if (!reader->ReadBytes(&frame->offset, offset_length)) { | |
| 1418 set_detailed_error("Unable to read offset."); | |
| 1419 return false; | |
| 1420 } | |
| 1421 | |
| 1422 // TODO(ianswett): Don't use StringPiece as an intermediary. | |
| 1423 StringPiece data; | |
| 1424 if (has_data_length) { | |
| 1425 if (!reader->ReadStringPiece16(&data)) { | |
| 1426 set_detailed_error("Unable to read frame data."); | |
| 1427 return false; | |
| 1428 } | |
| 1429 } else { | |
| 1430 if (!reader->ReadStringPiece(&data, reader->BytesRemaining())) { | |
| 1431 set_detailed_error("Unable to read frame data."); | |
| 1432 return false; | |
| 1433 } | |
| 1434 } | |
| 1435 frame->data_buffer = data.data(); | |
| 1436 frame->data_length = static_cast<uint16_t>(data.length()); | |
| 1437 | |
| 1438 return true; | |
| 1439 } | |
| 1440 | |
| 1441 bool QuicFramer::ProcessAckFrame(QuicDataReader* reader, | |
| 1442 uint8_t frame_type, | |
| 1443 QuicAckFrame* ack_frame) { | |
| 1444 // Determine the three lengths from the frame type: largest observed length, | |
| 1445 // missing packet number length, and missing range length. | |
| 1446 const QuicPacketNumberLength missing_packet_number_length = | |
| 1447 ReadSequenceNumberLength(frame_type); | |
| 1448 frame_type >>= kQuicSequenceNumberLengthShift; | |
| 1449 const QuicPacketNumberLength largest_observed_packet_number_length = | |
| 1450 ReadSequenceNumberLength(frame_type); | |
| 1451 frame_type >>= kQuicSequenceNumberLengthShift; | |
| 1452 ack_frame->is_truncated = frame_type & kQuicAckTruncatedMask; | |
| 1453 frame_type >>= kQuicAckTruncatedShift; | |
| 1454 bool has_nacks = frame_type & kQuicHasNacksMask; | |
| 1455 | |
| 1456 if (!reader->ReadBytes(&ack_frame->entropy_hash, 1)) { | |
| 1457 set_detailed_error("Unable to read entropy hash for received packets."); | |
| 1458 return false; | |
| 1459 } | |
| 1460 | |
| 1461 if (!reader->ReadBytes(&ack_frame->largest_observed, | |
| 1462 largest_observed_packet_number_length)) { | |
| 1463 set_detailed_error("Unable to read largest observed."); | |
| 1464 return false; | |
| 1465 } | |
| 1466 | |
| 1467 uint64_t ack_delay_time_us; | |
| 1468 if (!reader->ReadUFloat16(&ack_delay_time_us)) { | |
| 1469 set_detailed_error("Unable to read ack delay time."); | |
| 1470 return false; | |
| 1471 } | |
| 1472 | |
| 1473 if (ack_delay_time_us == kUFloat16MaxValue) { | |
| 1474 ack_frame->ack_delay_time = QuicTime::Delta::Infinite(); | |
| 1475 } else { | |
| 1476 ack_frame->ack_delay_time = | |
| 1477 QuicTime::Delta::FromMicroseconds(ack_delay_time_us); | |
| 1478 } | |
| 1479 | |
| 1480 if (!ProcessTimestampsInAckFrame(reader, ack_frame)) { | |
| 1481 return false; | |
| 1482 } | |
| 1483 | |
| 1484 if (!has_nacks) { | |
| 1485 return true; | |
| 1486 } | |
| 1487 | |
| 1488 uint8_t num_missing_ranges; | |
| 1489 if (!reader->ReadBytes(&num_missing_ranges, 1)) { | |
| 1490 set_detailed_error("Unable to read num missing packet ranges."); | |
| 1491 return false; | |
| 1492 } | |
| 1493 | |
| 1494 QuicPacketNumber last_packet_number = ack_frame->largest_observed; | |
| 1495 for (size_t i = 0; i < num_missing_ranges; ++i) { | |
| 1496 QuicPacketNumber missing_delta = 0; | |
| 1497 if (!reader->ReadBytes(&missing_delta, missing_packet_number_length)) { | |
| 1498 set_detailed_error("Unable to read missing packet number delta."); | |
| 1499 return false; | |
| 1500 } | |
| 1501 last_packet_number -= missing_delta; | |
| 1502 QuicPacketNumber range_length = 0; | |
| 1503 if (!reader->ReadBytes(&range_length, PACKET_1BYTE_PACKET_NUMBER)) { | |
| 1504 set_detailed_error("Unable to read missing packet number range."); | |
| 1505 return false; | |
| 1506 } | |
| 1507 ack_frame->packets.Add(last_packet_number - range_length, | |
| 1508 last_packet_number + 1); | |
| 1509 // Subtract an extra 1 to ensure ranges are represented efficiently and | |
| 1510 // can't overlap by 1 packet number. This allows a missing_delta of 0 | |
| 1511 // to represent an adjacent nack range. | |
| 1512 last_packet_number -= (range_length + 1); | |
| 1513 } | |
| 1514 | |
| 1515 if (quic_version_ > QUIC_VERSION_31) { | |
| 1516 return true; | |
| 1517 } | |
| 1518 | |
| 1519 // Parse the revived packets list. | |
| 1520 // TODO(ianswett): Change the ack frame so it only expresses one revived. | |
| 1521 uint8_t num_revived_packets; | |
| 1522 if (!reader->ReadBytes(&num_revived_packets, 1)) { | |
| 1523 set_detailed_error("Unable to read num revived packets."); | |
| 1524 return false; | |
| 1525 } | |
| 1526 | |
| 1527 for (size_t i = 0; i < num_revived_packets; ++i) { | |
| 1528 QuicPacketNumber revived_packet = 0; | |
| 1529 if (!reader->ReadBytes(&revived_packet, | |
| 1530 largest_observed_packet_number_length)) { | |
| 1531 set_detailed_error("Unable to read revived packet."); | |
| 1532 return false; | |
| 1533 } | |
| 1534 } | |
| 1535 | |
| 1536 return true; | |
| 1537 } | |
| 1538 | |
| 1539 bool QuicFramer::ProcessNewAckFrame(QuicDataReader* reader, | |
| 1540 uint8_t frame_type, | |
| 1541 QuicAckFrame* ack_frame) { | |
| 1542 // Determine the two lengths from the frame type: largest acked length, | |
| 1543 // ack block length. | |
| 1544 const QuicPacketNumberLength ack_block_length = | |
| 1545 ReadSequenceNumberLength(frame_type); | |
| 1546 frame_type >>= kQuicSequenceNumberLengthShift; | |
| 1547 const QuicPacketNumberLength largest_acked_length = | |
| 1548 ReadSequenceNumberLength(frame_type); | |
| 1549 frame_type >>= kQuicSequenceNumberLengthShift; | |
| 1550 frame_type >>= kQuicHasMultipleAckBlocksShift; | |
| 1551 bool has_ack_blocks = frame_type & kQuicHasMultipleAckBlocksMask; | |
| 1552 ack_frame->missing = false; | |
| 1553 | |
| 1554 if (!reader->ReadBytes(&ack_frame->largest_observed, largest_acked_length)) { | |
| 1555 set_detailed_error("Unable to read largest acked."); | |
| 1556 return false; | |
| 1557 } | |
| 1558 | |
| 1559 uint64_t ack_delay_time_us; | |
| 1560 if (!reader->ReadUFloat16(&ack_delay_time_us)) { | |
| 1561 set_detailed_error("Unable to read ack delay time."); | |
| 1562 return false; | |
| 1563 } | |
| 1564 | |
| 1565 if (ack_delay_time_us == kUFloat16MaxValue) { | |
| 1566 ack_frame->ack_delay_time = QuicTime::Delta::Infinite(); | |
| 1567 } else { | |
| 1568 ack_frame->ack_delay_time = | |
| 1569 QuicTime::Delta::FromMicroseconds(ack_delay_time_us); | |
| 1570 } | |
| 1571 | |
| 1572 uint8_t num_ack_blocks = 0; | |
| 1573 if (has_ack_blocks) { | |
| 1574 if (!reader->ReadBytes(&num_ack_blocks, 1)) { | |
| 1575 set_detailed_error("Unable to read num of ack blocks."); | |
| 1576 return false; | |
| 1577 } | |
| 1578 } | |
| 1579 | |
| 1580 size_t first_block_length = 0; | |
| 1581 if (!reader->ReadBytes(&first_block_length, ack_block_length)) { | |
| 1582 set_detailed_error("Unable to read first ack block length."); | |
| 1583 return false; | |
| 1584 } | |
| 1585 QuicPacketNumber first_received = | |
| 1586 ack_frame->largest_observed + 1 - first_block_length; | |
| 1587 ack_frame->packets.Add(first_received, ack_frame->largest_observed + 1); | |
| 1588 | |
| 1589 if (num_ack_blocks > 0) { | |
| 1590 for (size_t i = 0; i < num_ack_blocks; ++i) { | |
| 1591 size_t gap = 0; | |
| 1592 if (!reader->ReadBytes(&gap, PACKET_1BYTE_PACKET_NUMBER)) { | |
| 1593 set_detailed_error("Unable to read gap to next ack block."); | |
| 1594 return false; | |
| 1595 } | |
| 1596 size_t current_block_length = 0; | |
| 1597 if (!reader->ReadBytes(¤t_block_length, ack_block_length)) { | |
| 1598 set_detailed_error("Unable to ack block length."); | |
| 1599 return false; | |
| 1600 } | |
| 1601 first_received -= (gap + current_block_length); | |
| 1602 if (current_block_length > 0) { | |
| 1603 ack_frame->packets.Add(first_received, | |
| 1604 first_received + current_block_length); | |
| 1605 } | |
| 1606 } | |
| 1607 } | |
| 1608 | |
| 1609 if (!ProcessTimestampsInAckFrame(reader, ack_frame)) { | |
| 1610 return false; | |
| 1611 } | |
| 1612 | |
| 1613 return true; | |
| 1614 } | |
| 1615 | |
| 1616 bool QuicFramer::ProcessTimestampsInAckFrame(QuicDataReader* reader, | |
| 1617 QuicAckFrame* ack_frame) { | |
| 1618 if (ack_frame->is_truncated) { | |
| 1619 return true; | |
| 1620 } | |
| 1621 uint8_t num_received_packets; | |
| 1622 if (!reader->ReadBytes(&num_received_packets, 1)) { | |
| 1623 set_detailed_error("Unable to read num received packets."); | |
| 1624 return false; | |
| 1625 } | |
| 1626 | |
| 1627 if (num_received_packets > 0) { | |
| 1628 uint8_t delta_from_largest_observed; | |
| 1629 if (!reader->ReadBytes(&delta_from_largest_observed, | |
| 1630 PACKET_1BYTE_PACKET_NUMBER)) { | |
| 1631 set_detailed_error("Unable to read sequence delta in received packets."); | |
| 1632 return false; | |
| 1633 } | |
| 1634 QuicPacketNumber seq_num = | |
| 1635 ack_frame->largest_observed - delta_from_largest_observed; | |
| 1636 | |
| 1637 // Time delta from the framer creation. | |
| 1638 uint32_t time_delta_us; | |
| 1639 if (!reader->ReadBytes(&time_delta_us, sizeof(time_delta_us))) { | |
| 1640 set_detailed_error("Unable to read time delta in received packets."); | |
| 1641 return false; | |
| 1642 } | |
| 1643 | |
| 1644 last_timestamp_ = CalculateTimestampFromWire(time_delta_us); | |
| 1645 | |
| 1646 ack_frame->received_packet_times.reserve(num_received_packets); | |
| 1647 ack_frame->received_packet_times.push_back( | |
| 1648 std::make_pair(seq_num, creation_time_ + last_timestamp_)); | |
| 1649 | |
| 1650 for (uint8_t i = 1; i < num_received_packets; ++i) { | |
| 1651 if (!reader->ReadBytes(&delta_from_largest_observed, | |
| 1652 PACKET_1BYTE_PACKET_NUMBER)) { | |
| 1653 set_detailed_error( | |
| 1654 "Unable to read sequence delta in received packets."); | |
| 1655 return false; | |
| 1656 } | |
| 1657 seq_num = ack_frame->largest_observed - delta_from_largest_observed; | |
| 1658 | |
| 1659 // Time delta from the previous timestamp. | |
| 1660 uint64_t incremental_time_delta_us; | |
| 1661 if (!reader->ReadUFloat16(&incremental_time_delta_us)) { | |
| 1662 set_detailed_error( | |
| 1663 "Unable to read incremental time delta in received packets."); | |
| 1664 return false; | |
| 1665 } | |
| 1666 | |
| 1667 last_timestamp_ = last_timestamp_ + QuicTime::Delta::FromMicroseconds( | |
| 1668 incremental_time_delta_us); | |
| 1669 ack_frame->received_packet_times.push_back( | |
| 1670 std::make_pair(seq_num, creation_time_ + last_timestamp_)); | |
| 1671 } | |
| 1672 } | |
| 1673 return true; | |
| 1674 } | |
| 1675 | |
| 1676 bool QuicFramer::ProcessStopWaitingFrame(QuicDataReader* reader, | |
| 1677 const QuicPacketHeader& header, | |
| 1678 QuicStopWaitingFrame* stop_waiting) { | |
| 1679 if (quic_version_ <= QUIC_VERSION_33) { | |
| 1680 if (!reader->ReadBytes(&stop_waiting->entropy_hash, 1)) { | |
| 1681 set_detailed_error("Unable to read entropy hash for sent packets."); | |
| 1682 return false; | |
| 1683 } | |
| 1684 } | |
| 1685 | |
| 1686 QuicPacketNumber least_unacked_delta = 0; | |
| 1687 if (!reader->ReadBytes(&least_unacked_delta, | |
| 1688 header.public_header.packet_number_length)) { | |
| 1689 set_detailed_error("Unable to read least unacked delta."); | |
| 1690 return false; | |
| 1691 } | |
| 1692 DCHECK_GE(header.packet_number, least_unacked_delta); | |
| 1693 stop_waiting->least_unacked = header.packet_number - least_unacked_delta; | |
| 1694 | |
| 1695 return true; | |
| 1696 } | |
| 1697 | |
| 1698 bool QuicFramer::ProcessRstStreamFrame(QuicDataReader* reader, | |
| 1699 QuicRstStreamFrame* frame) { | |
| 1700 if (!reader->ReadUInt32(&frame->stream_id)) { | |
| 1701 set_detailed_error("Unable to read stream_id."); | |
| 1702 return false; | |
| 1703 } | |
| 1704 | |
| 1705 if (!reader->ReadUInt64(&frame->byte_offset)) { | |
| 1706 set_detailed_error("Unable to read rst stream sent byte offset."); | |
| 1707 return false; | |
| 1708 } | |
| 1709 | |
| 1710 uint32_t error_code; | |
| 1711 if (!reader->ReadUInt32(&error_code)) { | |
| 1712 set_detailed_error("Unable to read rst stream error code."); | |
| 1713 return false; | |
| 1714 } | |
| 1715 | |
| 1716 if (error_code >= QUIC_STREAM_LAST_ERROR) { | |
| 1717 // Ignore invalid stream error code if any. | |
| 1718 error_code = QUIC_STREAM_LAST_ERROR; | |
| 1719 } | |
| 1720 | |
| 1721 frame->error_code = static_cast<QuicRstStreamErrorCode>(error_code); | |
| 1722 return true; | |
| 1723 } | |
| 1724 | |
| 1725 bool QuicFramer::ProcessConnectionCloseFrame(QuicDataReader* reader, | |
| 1726 QuicConnectionCloseFrame* frame) { | |
| 1727 uint32_t error_code; | |
| 1728 if (!reader->ReadUInt32(&error_code)) { | |
| 1729 set_detailed_error("Unable to read connection close error code."); | |
| 1730 return false; | |
| 1731 } | |
| 1732 | |
| 1733 if (error_code >= QUIC_LAST_ERROR) { | |
| 1734 // Ignore invalid QUIC error code if any. | |
| 1735 error_code = QUIC_LAST_ERROR; | |
| 1736 } | |
| 1737 | |
| 1738 frame->error_code = static_cast<QuicErrorCode>(error_code); | |
| 1739 | |
| 1740 StringPiece error_details; | |
| 1741 if (!reader->ReadStringPiece16(&error_details)) { | |
| 1742 set_detailed_error("Unable to read connection close error details."); | |
| 1743 return false; | |
| 1744 } | |
| 1745 frame->error_details = error_details.as_string(); | |
| 1746 | |
| 1747 return true; | |
| 1748 } | |
| 1749 | |
| 1750 bool QuicFramer::ProcessGoAwayFrame(QuicDataReader* reader, | |
| 1751 QuicGoAwayFrame* frame) { | |
| 1752 uint32_t error_code; | |
| 1753 if (!reader->ReadUInt32(&error_code)) { | |
| 1754 set_detailed_error("Unable to read go away error code."); | |
| 1755 return false; | |
| 1756 } | |
| 1757 | |
| 1758 if (error_code >= QUIC_LAST_ERROR) { | |
| 1759 // Ignore invalid QUIC error code if any. | |
| 1760 error_code = QUIC_LAST_ERROR; | |
| 1761 } | |
| 1762 frame->error_code = static_cast<QuicErrorCode>(error_code); | |
| 1763 | |
| 1764 uint32_t stream_id; | |
| 1765 if (!reader->ReadUInt32(&stream_id)) { | |
| 1766 set_detailed_error("Unable to read last good stream id."); | |
| 1767 return false; | |
| 1768 } | |
| 1769 frame->last_good_stream_id = static_cast<QuicStreamId>(stream_id); | |
| 1770 | |
| 1771 StringPiece reason_phrase; | |
| 1772 if (!reader->ReadStringPiece16(&reason_phrase)) { | |
| 1773 set_detailed_error("Unable to read goaway reason."); | |
| 1774 return false; | |
| 1775 } | |
| 1776 frame->reason_phrase = reason_phrase.as_string(); | |
| 1777 | |
| 1778 return true; | |
| 1779 } | |
| 1780 | |
| 1781 bool QuicFramer::ProcessWindowUpdateFrame(QuicDataReader* reader, | |
| 1782 QuicWindowUpdateFrame* frame) { | |
| 1783 if (!reader->ReadUInt32(&frame->stream_id)) { | |
| 1784 set_detailed_error("Unable to read stream_id."); | |
| 1785 return false; | |
| 1786 } | |
| 1787 | |
| 1788 if (!reader->ReadUInt64(&frame->byte_offset)) { | |
| 1789 set_detailed_error("Unable to read window byte_offset."); | |
| 1790 return false; | |
| 1791 } | |
| 1792 | |
| 1793 return true; | |
| 1794 } | |
| 1795 | |
| 1796 bool QuicFramer::ProcessBlockedFrame(QuicDataReader* reader, | |
| 1797 QuicBlockedFrame* frame) { | |
| 1798 if (!reader->ReadUInt32(&frame->stream_id)) { | |
| 1799 set_detailed_error("Unable to read stream_id."); | |
| 1800 return false; | |
| 1801 } | |
| 1802 | |
| 1803 return true; | |
| 1804 } | |
| 1805 | |
| 1806 bool QuicFramer::ProcessPathCloseFrame(QuicDataReader* reader, | |
| 1807 QuicPathCloseFrame* frame) { | |
| 1808 if (!reader->ReadBytes(&frame->path_id, 1)) { | |
| 1809 set_detailed_error("Unable to read path_id."); | |
| 1810 return false; | |
| 1811 } | |
| 1812 | |
| 1813 return true; | |
| 1814 } | |
| 1815 | |
| 1816 // static | |
| 1817 StringPiece QuicFramer::GetAssociatedDataFromEncryptedPacket( | |
| 1818 QuicVersion version, | |
| 1819 const QuicEncryptedPacket& encrypted, | |
| 1820 QuicConnectionIdLength connection_id_length, | |
| 1821 bool includes_version, | |
| 1822 bool includes_path_id, | |
| 1823 bool includes_diversification_nonce, | |
| 1824 QuicPacketNumberLength packet_number_length) { | |
| 1825 // TODO(ianswett): This is identical to QuicData::AssociatedData. | |
| 1826 return StringPiece( | |
| 1827 encrypted.data(), | |
| 1828 GetStartOfEncryptedData(version, connection_id_length, includes_version, | |
| 1829 includes_path_id, includes_diversification_nonce, | |
| 1830 packet_number_length)); | |
| 1831 } | |
| 1832 | |
| 1833 void QuicFramer::SetDecrypter(EncryptionLevel level, QuicDecrypter* decrypter) { | |
| 1834 DCHECK(alternative_decrypter_.get() == nullptr); | |
| 1835 DCHECK_GE(level, decrypter_level_); | |
| 1836 decrypter_.reset(decrypter); | |
| 1837 decrypter_level_ = level; | |
| 1838 } | |
| 1839 | |
| 1840 void QuicFramer::SetAlternativeDecrypter(EncryptionLevel level, | |
| 1841 QuicDecrypter* decrypter, | |
| 1842 bool latch_once_used) { | |
| 1843 alternative_decrypter_.reset(decrypter); | |
| 1844 alternative_decrypter_level_ = level; | |
| 1845 alternative_decrypter_latch_ = latch_once_used; | |
| 1846 } | |
| 1847 | |
| 1848 const QuicDecrypter* QuicFramer::decrypter() const { | |
| 1849 return decrypter_.get(); | |
| 1850 } | |
| 1851 | |
| 1852 const QuicDecrypter* QuicFramer::alternative_decrypter() const { | |
| 1853 return alternative_decrypter_.get(); | |
| 1854 } | |
| 1855 | |
| 1856 void QuicFramer::SetEncrypter(EncryptionLevel level, QuicEncrypter* encrypter) { | |
| 1857 DCHECK_GE(level, 0); | |
| 1858 DCHECK_LT(level, NUM_ENCRYPTION_LEVELS); | |
| 1859 encrypter_[level].reset(encrypter); | |
| 1860 } | |
| 1861 | |
| 1862 size_t QuicFramer::EncryptInPlace(EncryptionLevel level, | |
| 1863 QuicPathId path_id, | |
| 1864 QuicPacketNumber packet_number, | |
| 1865 size_t ad_len, | |
| 1866 size_t total_len, | |
| 1867 size_t buffer_len, | |
| 1868 char* buffer) { | |
| 1869 size_t output_length = 0; | |
| 1870 if (!encrypter_[level]->EncryptPacket( | |
| 1871 path_id, packet_number, | |
| 1872 StringPiece(buffer, ad_len), // Associated data | |
| 1873 StringPiece(buffer + ad_len, total_len - ad_len), // Plaintext | |
| 1874 buffer + ad_len, // Destination buffer | |
| 1875 &output_length, buffer_len - ad_len)) { | |
| 1876 RaiseError(QUIC_ENCRYPTION_FAILURE); | |
| 1877 return 0; | |
| 1878 } | |
| 1879 | |
| 1880 return ad_len + output_length; | |
| 1881 } | |
| 1882 | |
| 1883 size_t QuicFramer::EncryptPayload(EncryptionLevel level, | |
| 1884 QuicPathId path_id, | |
| 1885 QuicPacketNumber packet_number, | |
| 1886 const QuicPacket& packet, | |
| 1887 char* buffer, | |
| 1888 size_t buffer_len) { | |
| 1889 DCHECK(encrypter_[level].get() != nullptr); | |
| 1890 | |
| 1891 StringPiece associated_data = packet.AssociatedData(quic_version_); | |
| 1892 // Copy in the header, because the encrypter only populates the encrypted | |
| 1893 // plaintext content. | |
| 1894 const size_t ad_len = associated_data.length(); | |
| 1895 memmove(buffer, associated_data.data(), ad_len); | |
| 1896 // Encrypt the plaintext into the buffer. | |
| 1897 size_t output_length = 0; | |
| 1898 if (!encrypter_[level]->EncryptPacket(path_id, packet_number, associated_data, | |
| 1899 packet.Plaintext(quic_version_), | |
| 1900 buffer + ad_len, &output_length, | |
| 1901 buffer_len - ad_len)) { | |
| 1902 RaiseError(QUIC_ENCRYPTION_FAILURE); | |
| 1903 return 0; | |
| 1904 } | |
| 1905 | |
| 1906 return ad_len + output_length; | |
| 1907 } | |
| 1908 | |
| 1909 size_t QuicFramer::GetMaxPlaintextSize(size_t ciphertext_size) { | |
| 1910 // In order to keep the code simple, we don't have the current encryption | |
| 1911 // level to hand. Both the NullEncrypter and AES-GCM have a tag length of 12. | |
| 1912 size_t min_plaintext_size = ciphertext_size; | |
| 1913 | |
| 1914 for (int i = ENCRYPTION_NONE; i < NUM_ENCRYPTION_LEVELS; i++) { | |
| 1915 if (encrypter_[i].get() != nullptr) { | |
| 1916 size_t size = encrypter_[i]->GetMaxPlaintextSize(ciphertext_size); | |
| 1917 if (size < min_plaintext_size) { | |
| 1918 min_plaintext_size = size; | |
| 1919 } | |
| 1920 } | |
| 1921 } | |
| 1922 | |
| 1923 return min_plaintext_size; | |
| 1924 } | |
| 1925 | |
| 1926 bool QuicFramer::DecryptPayload(QuicDataReader* encrypted_reader, | |
| 1927 const QuicPacketHeader& header, | |
| 1928 const QuicEncryptedPacket& packet, | |
| 1929 char* decrypted_buffer, | |
| 1930 size_t buffer_length, | |
| 1931 size_t* decrypted_length) { | |
| 1932 StringPiece encrypted = encrypted_reader->ReadRemainingPayload(); | |
| 1933 DCHECK(decrypter_.get() != nullptr); | |
| 1934 StringPiece associated_data = GetAssociatedDataFromEncryptedPacket( | |
| 1935 quic_version_, packet, header.public_header.connection_id_length, | |
| 1936 header.public_header.version_flag, header.public_header.multipath_flag, | |
| 1937 header.public_header.nonce != nullptr, | |
| 1938 header.public_header.packet_number_length); | |
| 1939 | |
| 1940 bool success = decrypter_->DecryptPacket( | |
| 1941 header.path_id, header.packet_number, associated_data, encrypted, | |
| 1942 decrypted_buffer, decrypted_length, buffer_length); | |
| 1943 if (success) { | |
| 1944 visitor_->OnDecryptedPacket(decrypter_level_); | |
| 1945 } else if (alternative_decrypter_.get() != nullptr) { | |
| 1946 if (header.public_header.nonce != nullptr) { | |
| 1947 DCHECK_EQ(perspective_, Perspective::IS_CLIENT); | |
| 1948 alternative_decrypter_->SetDiversificationNonce( | |
| 1949 *header.public_header.nonce); | |
| 1950 } | |
| 1951 bool try_alternative_decryption = true; | |
| 1952 if (alternative_decrypter_level_ == ENCRYPTION_INITIAL) { | |
| 1953 if (perspective_ == Perspective::IS_CLIENT && | |
| 1954 quic_version_ > QUIC_VERSION_32) { | |
| 1955 if (header.public_header.nonce == nullptr) { | |
| 1956 // Can not use INITIAL decryption without a diversification nonce. | |
| 1957 try_alternative_decryption = false; | |
| 1958 } | |
| 1959 } else { | |
| 1960 DCHECK(header.public_header.nonce == nullptr); | |
| 1961 } | |
| 1962 } | |
| 1963 | |
| 1964 if (try_alternative_decryption) { | |
| 1965 success = alternative_decrypter_->DecryptPacket( | |
| 1966 header.path_id, header.packet_number, associated_data, encrypted, | |
| 1967 decrypted_buffer, decrypted_length, buffer_length); | |
| 1968 } | |
| 1969 if (success) { | |
| 1970 visitor_->OnDecryptedPacket(alternative_decrypter_level_); | |
| 1971 if (alternative_decrypter_latch_) { | |
| 1972 // Switch to the alternative decrypter and latch so that we cannot | |
| 1973 // switch back. | |
| 1974 decrypter_.reset(alternative_decrypter_.release()); | |
| 1975 decrypter_level_ = alternative_decrypter_level_; | |
| 1976 alternative_decrypter_level_ = ENCRYPTION_NONE; | |
| 1977 } else { | |
| 1978 // Switch the alternative decrypter so that we use it first next time. | |
| 1979 decrypter_.swap(alternative_decrypter_); | |
| 1980 EncryptionLevel level = alternative_decrypter_level_; | |
| 1981 alternative_decrypter_level_ = decrypter_level_; | |
| 1982 decrypter_level_ = level; | |
| 1983 } | |
| 1984 } | |
| 1985 } | |
| 1986 | |
| 1987 if (!success) { | |
| 1988 DLOG(WARNING) << "DecryptPacket failed for packet_number:" | |
| 1989 << header.packet_number; | |
| 1990 return false; | |
| 1991 } | |
| 1992 | |
| 1993 return true; | |
| 1994 } | |
| 1995 | |
| 1996 size_t QuicFramer::GetAckFrameTimeStampSize(const QuicAckFrame& ack) { | |
| 1997 if (ack.received_packet_times.empty()) { | |
| 1998 return 0; | |
| 1999 } | |
| 2000 | |
| 2001 return 5 + 3 * (ack.received_packet_times.size() - 1); | |
| 2002 } | |
| 2003 | |
| 2004 size_t QuicFramer::GetAckFrameSize( | |
| 2005 const QuicAckFrame& ack, | |
| 2006 QuicPacketNumberLength packet_number_length) { | |
| 2007 size_t ack_size = 0; | |
| 2008 if (quic_version_ <= QUIC_VERSION_33) { | |
| 2009 AckFrameInfo ack_info = GetAckFrameInfo(ack); | |
| 2010 QuicPacketNumberLength largest_observed_length = | |
| 2011 GetMinSequenceNumberLength(ack.largest_observed); | |
| 2012 QuicPacketNumberLength missing_packet_number_length = | |
| 2013 GetMinSequenceNumberLength(ack_info.max_delta); | |
| 2014 | |
| 2015 ack_size = GetMinAckFrameSize(quic_version_, largest_observed_length); | |
| 2016 if (!ack_info.nack_ranges.empty()) { | |
| 2017 ack_size += kNumberOfNackRangesSize; | |
| 2018 if (quic_version_ <= QUIC_VERSION_31) { | |
| 2019 ack_size += kNumberOfRevivedPacketsSize; | |
| 2020 } | |
| 2021 ack_size += min(ack_info.nack_ranges.size(), kMaxNackRanges) * | |
| 2022 (missing_packet_number_length + PACKET_1BYTE_PACKET_NUMBER); | |
| 2023 } | |
| 2024 | |
| 2025 // In version 23, if the ack will be truncated due to too many nack ranges, | |
| 2026 // then do not include the number of timestamps (1 byte). | |
| 2027 if (ack_info.nack_ranges.size() <= kMaxNackRanges) { | |
| 2028 // 1 byte for the number of timestamps. | |
| 2029 ack_size += 1; | |
| 2030 ack_size += GetAckFrameTimeStampSize(ack); | |
| 2031 } | |
| 2032 | |
| 2033 return ack_size; | |
| 2034 } | |
| 2035 | |
| 2036 NewAckFrameInfo ack_info = | |
| 2037 GetNewAckFrameInfo(ack, !FLAGS_quic_use_packet_number_queue_intervals); | |
| 2038 QuicPacketNumberLength largest_acked_length = | |
| 2039 GetMinSequenceNumberLength(ack.largest_observed); | |
| 2040 QuicPacketNumberLength ack_block_length = | |
| 2041 GetMinSequenceNumberLength(ack_info.max_block_length); | |
| 2042 | |
| 2043 ack_size = GetMinAckFrameSize(quic_version_, largest_acked_length); | |
| 2044 // First ack block length. | |
| 2045 ack_size += ack_block_length; | |
| 2046 if (ack_info.num_ack_blocks != 0) { | |
| 2047 ack_size += kNumberOfAckBlocksSize; | |
| 2048 ack_size += min(ack_info.num_ack_blocks, kMaxAckBlocks) * | |
| 2049 (ack_block_length + PACKET_1BYTE_PACKET_NUMBER); | |
| 2050 } | |
| 2051 | |
| 2052 // Include timestamps. | |
| 2053 ack_size += GetAckFrameTimeStampSize(ack); | |
| 2054 | |
| 2055 return ack_size; | |
| 2056 } | |
| 2057 | |
| 2058 size_t QuicFramer::ComputeFrameLength( | |
| 2059 const QuicFrame& frame, | |
| 2060 bool last_frame_in_packet, | |
| 2061 QuicPacketNumberLength packet_number_length) { | |
| 2062 switch (frame.type) { | |
| 2063 case STREAM_FRAME: | |
| 2064 return GetMinStreamFrameSize(frame.stream_frame->stream_id, | |
| 2065 frame.stream_frame->offset, | |
| 2066 last_frame_in_packet) + | |
| 2067 frame.stream_frame->data_length; | |
| 2068 case ACK_FRAME: { | |
| 2069 return GetAckFrameSize(*frame.ack_frame, packet_number_length); | |
| 2070 } | |
| 2071 case STOP_WAITING_FRAME: | |
| 2072 return GetStopWaitingFrameSize(quic_version_, packet_number_length); | |
| 2073 case MTU_DISCOVERY_FRAME: | |
| 2074 // MTU discovery frames are serialized as ping frames. | |
| 2075 case PING_FRAME: | |
| 2076 // Ping has no payload. | |
| 2077 return kQuicFrameTypeSize; | |
| 2078 case RST_STREAM_FRAME: | |
| 2079 return GetRstStreamFrameSize(); | |
| 2080 case CONNECTION_CLOSE_FRAME: | |
| 2081 return GetMinConnectionCloseFrameSize() + | |
| 2082 frame.connection_close_frame->error_details.size(); | |
| 2083 case GOAWAY_FRAME: | |
| 2084 return GetMinGoAwayFrameSize() + frame.goaway_frame->reason_phrase.size(); | |
| 2085 case WINDOW_UPDATE_FRAME: | |
| 2086 return GetWindowUpdateFrameSize(); | |
| 2087 case BLOCKED_FRAME: | |
| 2088 return GetBlockedFrameSize(); | |
| 2089 case PATH_CLOSE_FRAME: | |
| 2090 return GetPathCloseFrameSize(); | |
| 2091 case PADDING_FRAME: | |
| 2092 DCHECK(false); | |
| 2093 return 0; | |
| 2094 case NUM_FRAME_TYPES: | |
| 2095 DCHECK(false); | |
| 2096 return 0; | |
| 2097 } | |
| 2098 | |
| 2099 // Not reachable, but some Chrome compilers can't figure that out. *sigh* | |
| 2100 DCHECK(false); | |
| 2101 return 0; | |
| 2102 } | |
| 2103 | |
| 2104 bool QuicFramer::AppendTypeByte(const QuicFrame& frame, | |
| 2105 bool no_stream_frame_length, | |
| 2106 QuicDataWriter* writer) { | |
| 2107 uint8_t type_byte = 0; | |
| 2108 switch (frame.type) { | |
| 2109 case STREAM_FRAME: { | |
| 2110 if (frame.stream_frame == nullptr) { | |
| 2111 QUIC_BUG << "Failed to append STREAM frame with no stream_frame."; | |
| 2112 } | |
| 2113 // Fin bit. | |
| 2114 type_byte |= frame.stream_frame->fin ? kQuicStreamFinMask : 0; | |
| 2115 | |
| 2116 // Data Length bit. | |
| 2117 type_byte <<= kQuicStreamDataLengthShift; | |
| 2118 type_byte |= no_stream_frame_length ? 0 : kQuicStreamDataLengthMask; | |
| 2119 | |
| 2120 // Offset 3 bits. | |
| 2121 type_byte <<= kQuicStreamOffsetShift; | |
| 2122 const size_t offset_len = GetStreamOffsetSize(frame.stream_frame->offset); | |
| 2123 if (offset_len > 0) { | |
| 2124 type_byte |= offset_len - 1; | |
| 2125 } | |
| 2126 | |
| 2127 // stream id 2 bits. | |
| 2128 type_byte <<= kQuicStreamIdShift; | |
| 2129 type_byte |= GetStreamIdSize(frame.stream_frame->stream_id) - 1; | |
| 2130 type_byte |= kQuicFrameTypeStreamMask; // Set Stream Frame Type to 1. | |
| 2131 break; | |
| 2132 } | |
| 2133 case ACK_FRAME: | |
| 2134 return true; | |
| 2135 case MTU_DISCOVERY_FRAME: | |
| 2136 type_byte = static_cast<uint8_t>(PING_FRAME); | |
| 2137 break; | |
| 2138 default: | |
| 2139 type_byte = static_cast<uint8_t>(frame.type); | |
| 2140 break; | |
| 2141 } | |
| 2142 | |
| 2143 return writer->WriteUInt8(type_byte); | |
| 2144 } | |
| 2145 | |
| 2146 // static | |
| 2147 bool QuicFramer::AppendPacketSequenceNumber( | |
| 2148 QuicPacketNumberLength packet_number_length, | |
| 2149 QuicPacketNumber packet_number, | |
| 2150 QuicDataWriter* writer) { | |
| 2151 // Ensure the entire packet number can be written. | |
| 2152 if (writer->capacity() - writer->length() < | |
| 2153 static_cast<size_t>(packet_number_length)) { | |
| 2154 return false; | |
| 2155 } | |
| 2156 switch (packet_number_length) { | |
| 2157 case PACKET_1BYTE_PACKET_NUMBER: | |
| 2158 return writer->WriteUInt8(packet_number & k1ByteSequenceNumberMask); | |
| 2159 break; | |
| 2160 case PACKET_2BYTE_PACKET_NUMBER: | |
| 2161 return writer->WriteUInt16(packet_number & k2ByteSequenceNumberMask); | |
| 2162 break; | |
| 2163 case PACKET_4BYTE_PACKET_NUMBER: | |
| 2164 return writer->WriteUInt32(packet_number & k4ByteSequenceNumberMask); | |
| 2165 break; | |
| 2166 case PACKET_6BYTE_PACKET_NUMBER: | |
| 2167 return writer->WriteUInt48(packet_number & k6ByteSequenceNumberMask); | |
| 2168 break; | |
| 2169 default: | |
| 2170 DCHECK(false) << "packet_number_length: " << packet_number_length; | |
| 2171 return false; | |
| 2172 } | |
| 2173 } | |
| 2174 | |
| 2175 // static | |
| 2176 bool QuicFramer::AppendAckBlock(uint8_t gap, | |
| 2177 QuicPacketNumberLength length_length, | |
| 2178 QuicPacketNumber length, | |
| 2179 QuicDataWriter* writer) { | |
| 2180 return AppendPacketSequenceNumber(PACKET_1BYTE_PACKET_NUMBER, gap, writer) && | |
| 2181 AppendPacketSequenceNumber(length_length, length, writer); | |
| 2182 } | |
| 2183 | |
| 2184 bool QuicFramer::AppendStreamFrame(const QuicStreamFrame& frame, | |
| 2185 bool no_stream_frame_length, | |
| 2186 QuicDataWriter* writer) { | |
| 2187 if (!writer->WriteBytes(&frame.stream_id, GetStreamIdSize(frame.stream_id))) { | |
| 2188 QUIC_BUG << "Writing stream id size failed."; | |
| 2189 return false; | |
| 2190 } | |
| 2191 if (!writer->WriteBytes(&frame.offset, GetStreamOffsetSize(frame.offset))) { | |
| 2192 QUIC_BUG << "Writing offset size failed."; | |
| 2193 return false; | |
| 2194 } | |
| 2195 if (!no_stream_frame_length) { | |
| 2196 if ((frame.data_length > numeric_limits<uint16_t>::max()) || | |
| 2197 !writer->WriteUInt16(static_cast<uint16_t>(frame.data_length))) { | |
| 2198 QUIC_BUG << "Writing stream frame length failed"; | |
| 2199 return false; | |
| 2200 } | |
| 2201 } | |
| 2202 | |
| 2203 if (!writer->WriteBytes(frame.data_buffer, frame.data_length)) { | |
| 2204 QUIC_BUG << "Writing frame data failed."; | |
| 2205 return false; | |
| 2206 } | |
| 2207 return true; | |
| 2208 } | |
| 2209 | |
| 2210 void QuicFramer::set_version(const QuicVersion version) { | |
| 2211 DCHECK(IsSupportedVersion(version)) << QuicVersionToString(version); | |
| 2212 quic_version_ = version; | |
| 2213 } | |
| 2214 | |
| 2215 bool QuicFramer::AppendAckFrameAndTypeByte(const QuicPacketHeader& header, | |
| 2216 const QuicAckFrame& frame, | |
| 2217 QuicDataWriter* writer) { | |
| 2218 AckFrameInfo ack_info = GetAckFrameInfo(frame); | |
| 2219 QuicPacketNumber ack_largest_observed = frame.largest_observed; | |
| 2220 QuicPacketNumberLength largest_observed_length = | |
| 2221 GetMinSequenceNumberLength(ack_largest_observed); | |
| 2222 QuicPacketNumberLength missing_packet_number_length = | |
| 2223 GetMinSequenceNumberLength(ack_info.max_delta); | |
| 2224 // Determine whether we need to truncate ranges. | |
| 2225 size_t available_range_bytes = | |
| 2226 writer->capacity() - writer->length() - kNumberOfNackRangesSize - | |
| 2227 GetMinAckFrameSize(quic_version_, largest_observed_length); | |
| 2228 if (quic_version_ <= QUIC_VERSION_31) { | |
| 2229 available_range_bytes -= kNumberOfRevivedPacketsSize; | |
| 2230 } | |
| 2231 size_t max_num_ranges = | |
| 2232 available_range_bytes / | |
| 2233 (missing_packet_number_length + PACKET_1BYTE_PACKET_NUMBER); | |
| 2234 max_num_ranges = min(kMaxNackRanges, max_num_ranges); | |
| 2235 bool truncated = ack_info.nack_ranges.size() > max_num_ranges; | |
| 2236 DVLOG_IF(1, truncated) << "Truncating ack from " | |
| 2237 << ack_info.nack_ranges.size() << " ranges to " | |
| 2238 << max_num_ranges; | |
| 2239 // Write out the type byte by setting the low order bits and doing shifts | |
| 2240 // to make room for the next bit flags to be set. | |
| 2241 // Whether there are any nacks. | |
| 2242 uint8_t type_byte = ack_info.nack_ranges.empty() ? 0 : kQuicHasNacksMask; | |
| 2243 | |
| 2244 // truncating bit. | |
| 2245 type_byte <<= kQuicAckTruncatedShift; | |
| 2246 type_byte |= truncated ? kQuicAckTruncatedMask : 0; | |
| 2247 | |
| 2248 // Largest observed packet number length. | |
| 2249 type_byte <<= kQuicSequenceNumberLengthShift; | |
| 2250 type_byte |= GetSequenceNumberFlags(largest_observed_length); | |
| 2251 | |
| 2252 // Missing packet number length. | |
| 2253 type_byte <<= kQuicSequenceNumberLengthShift; | |
| 2254 type_byte |= GetSequenceNumberFlags(missing_packet_number_length); | |
| 2255 | |
| 2256 type_byte |= kQuicFrameTypeAckMask; | |
| 2257 | |
| 2258 if (!writer->WriteUInt8(type_byte)) { | |
| 2259 QUIC_BUG << "type byte failed"; | |
| 2260 return false; | |
| 2261 } | |
| 2262 | |
| 2263 QuicPacketEntropyHash ack_entropy_hash = frame.entropy_hash; | |
| 2264 NackRangeMap::reverse_iterator ack_iter = ack_info.nack_ranges.rbegin(); | |
| 2265 if (truncated) { | |
| 2266 // Skip the nack ranges which the truncated ack won't include and set | |
| 2267 // a correct largest observed for the truncated ack. | |
| 2268 for (size_t i = 1; i < (ack_info.nack_ranges.size() - max_num_ranges); | |
| 2269 ++i) { | |
| 2270 ++ack_iter; | |
| 2271 } | |
| 2272 // If the last range is followed by acks, include them. | |
| 2273 // If the last range is followed by another range, specify the end of the | |
| 2274 // range as the largest_observed. | |
| 2275 ack_largest_observed = ack_iter->first - 1; | |
| 2276 // Also update the entropy so it matches the largest observed. | |
| 2277 ack_entropy_hash = entropy_calculator_->EntropyHash(ack_largest_observed); | |
| 2278 ++ack_iter; | |
| 2279 } | |
| 2280 | |
| 2281 if (!writer->WriteUInt8(ack_entropy_hash)) { | |
| 2282 QUIC_BUG << "hash failed."; | |
| 2283 return false; | |
| 2284 } | |
| 2285 | |
| 2286 if (!AppendPacketSequenceNumber(largest_observed_length, ack_largest_observed, | |
| 2287 writer)) { | |
| 2288 QUIC_BUG << "AppendPacketSequenceNumber failed. " | |
| 2289 << "largest_observed_length: " << largest_observed_length | |
| 2290 << " ack_largest_observed: " << ack_largest_observed; | |
| 2291 return false; | |
| 2292 } | |
| 2293 | |
| 2294 uint64_t ack_delay_time_us = kUFloat16MaxValue; | |
| 2295 if (!frame.ack_delay_time.IsInfinite()) { | |
| 2296 DCHECK_LE(0u, frame.ack_delay_time.ToMicroseconds()); | |
| 2297 ack_delay_time_us = frame.ack_delay_time.ToMicroseconds(); | |
| 2298 } | |
| 2299 | |
| 2300 if (!writer->WriteUFloat16(ack_delay_time_us)) { | |
| 2301 QUIC_BUG << "ack delay time failed."; | |
| 2302 return false; | |
| 2303 } | |
| 2304 | |
| 2305 // Timestamp goes at the end of the required fields. | |
| 2306 if (!truncated) { | |
| 2307 if (!AppendTimestampToAckFrame(frame, writer)) { | |
| 2308 QUIC_BUG << "AppendTimestampToAckFrame failed"; | |
| 2309 return false; | |
| 2310 } | |
| 2311 } | |
| 2312 | |
| 2313 if (ack_info.nack_ranges.empty()) { | |
| 2314 return true; | |
| 2315 } | |
| 2316 | |
| 2317 const uint8_t num_missing_ranges = | |
| 2318 static_cast<uint8_t>(min(ack_info.nack_ranges.size(), max_num_ranges)); | |
| 2319 if (!writer->WriteBytes(&num_missing_ranges, 1)) { | |
| 2320 QUIC_BUG << "num_missing_ranges failed: " | |
| 2321 << static_cast<uint32_t>(num_missing_ranges); | |
| 2322 return false; | |
| 2323 } | |
| 2324 | |
| 2325 int num_ranges_written = 0; | |
| 2326 QuicPacketNumber last_sequence_written = ack_largest_observed; | |
| 2327 for (; ack_iter != ack_info.nack_ranges.rend(); ++ack_iter) { | |
| 2328 // Calculate the delta to the last number in the range. | |
| 2329 QuicPacketNumber missing_delta = | |
| 2330 last_sequence_written - (ack_iter->first + ack_iter->second); | |
| 2331 if (!AppendPacketSequenceNumber(missing_packet_number_length, missing_delta, | |
| 2332 writer)) { | |
| 2333 QUIC_BUG << "AppendPacketSequenceNumber failed: " | |
| 2334 << "missing_packet_number_length: " | |
| 2335 << missing_packet_number_length << " missing_delta " | |
| 2336 << missing_delta; | |
| 2337 return false; | |
| 2338 } | |
| 2339 if (!AppendPacketSequenceNumber(PACKET_1BYTE_PACKET_NUMBER, | |
| 2340 ack_iter->second, writer)) { | |
| 2341 QUIC_BUG << "AppendPacketSequenceNumber failed"; | |
| 2342 return false; | |
| 2343 } | |
| 2344 // Subtract 1 so a missing_delta of 0 means an adjacent range. | |
| 2345 last_sequence_written = ack_iter->first - 1; | |
| 2346 ++num_ranges_written; | |
| 2347 } | |
| 2348 DCHECK_EQ(num_missing_ranges, num_ranges_written); | |
| 2349 | |
| 2350 if (quic_version_ > QUIC_VERSION_31) { | |
| 2351 return true; | |
| 2352 } | |
| 2353 | |
| 2354 // Append revived packets. | |
| 2355 // FEC is not supported. | |
| 2356 uint8_t num_revived_packets = 0; | |
| 2357 if (!writer->WriteBytes(&num_revived_packets, 1)) { | |
| 2358 QUIC_BUG << "num_revived_packets failed: " << num_revived_packets; | |
| 2359 return false; | |
| 2360 } | |
| 2361 | |
| 2362 return true; | |
| 2363 } | |
| 2364 | |
| 2365 bool QuicFramer::AppendNewAckFrameAndTypeByte(const QuicAckFrame& frame, | |
| 2366 QuicDataWriter* writer) { | |
| 2367 const bool new_ack_info_construct_blocks = | |
| 2368 !FLAGS_quic_use_packet_number_queue_intervals; | |
| 2369 const NewAckFrameInfo new_ack_info = | |
| 2370 GetNewAckFrameInfo(frame, new_ack_info_construct_blocks); | |
| 2371 QuicPacketNumber largest_acked = frame.largest_observed; | |
| 2372 QuicPacketNumberLength largest_acked_length = | |
| 2373 GetMinSequenceNumberLength(largest_acked); | |
| 2374 QuicPacketNumberLength ack_block_length = | |
| 2375 GetMinSequenceNumberLength(new_ack_info.max_block_length); | |
| 2376 // Calculate available bytes for timestamps and ack blocks. | |
| 2377 int32_t available_timestamp_and_ack_block_bytes = | |
| 2378 writer->capacity() - writer->length() - ack_block_length - | |
| 2379 GetMinAckFrameSize(quic_version_, largest_acked_length) - | |
| 2380 (new_ack_info.num_ack_blocks != 0 ? kNumberOfAckBlocksSize : 0); | |
| 2381 DCHECK_LE(0, available_timestamp_and_ack_block_bytes); | |
| 2382 | |
| 2383 // Write out the type byte by setting the low order bits and doing shifts | |
| 2384 // to make room for the next bit flags to be set. | |
| 2385 // Whether there are multiple ack blocks. | |
| 2386 uint8_t type_byte = | |
| 2387 new_ack_info.num_ack_blocks == 0 ? 0 : kQuicHasMultipleAckBlocksMask; | |
| 2388 type_byte <<= kQuicHasMultipleAckBlocksShift; | |
| 2389 | |
| 2390 // Largest acked length. | |
| 2391 type_byte <<= kQuicSequenceNumberLengthShift; | |
| 2392 type_byte |= GetSequenceNumberFlags(largest_acked_length); | |
| 2393 | |
| 2394 // Ack block length. | |
| 2395 type_byte <<= kQuicSequenceNumberLengthShift; | |
| 2396 type_byte |= GetSequenceNumberFlags(ack_block_length); | |
| 2397 | |
| 2398 type_byte |= kQuicFrameTypeAckMask; | |
| 2399 | |
| 2400 if (!writer->WriteUInt8(type_byte)) { | |
| 2401 return false; | |
| 2402 } | |
| 2403 | |
| 2404 // Largest acked. | |
| 2405 if (!AppendPacketSequenceNumber(largest_acked_length, largest_acked, | |
| 2406 writer)) { | |
| 2407 return false; | |
| 2408 } | |
| 2409 | |
| 2410 // Largest acked delta time. | |
| 2411 uint64_t ack_delay_time_us = kUFloat16MaxValue; | |
| 2412 if (!frame.ack_delay_time.IsInfinite()) { | |
| 2413 DCHECK_LE(0u, frame.ack_delay_time.ToMicroseconds()); | |
| 2414 ack_delay_time_us = frame.ack_delay_time.ToMicroseconds(); | |
| 2415 } | |
| 2416 if (!writer->WriteUFloat16(ack_delay_time_us)) { | |
| 2417 return false; | |
| 2418 } | |
| 2419 | |
| 2420 size_t max_num_ack_blocks = available_timestamp_and_ack_block_bytes / | |
| 2421 (ack_block_length + PACKET_1BYTE_PACKET_NUMBER); | |
| 2422 | |
| 2423 // Number of ack blocks. | |
| 2424 size_t num_ack_blocks = min(new_ack_info.num_ack_blocks, max_num_ack_blocks); | |
| 2425 if (num_ack_blocks > numeric_limits<uint8_t>::max()) { | |
| 2426 num_ack_blocks = numeric_limits<uint8_t>::max(); | |
| 2427 } | |
| 2428 | |
| 2429 if (num_ack_blocks > 0) { | |
| 2430 if (!writer->WriteBytes(&num_ack_blocks, 1)) { | |
| 2431 return false; | |
| 2432 } | |
| 2433 } | |
| 2434 | |
| 2435 // First ack block length. | |
| 2436 if (!AppendPacketSequenceNumber(ack_block_length, | |
| 2437 new_ack_info.first_block_length, writer)) { | |
| 2438 return false; | |
| 2439 } | |
| 2440 | |
| 2441 // Ack blocks. | |
| 2442 if (num_ack_blocks > 0) { | |
| 2443 size_t num_ack_blocks_written = 0; | |
| 2444 if (!new_ack_info_construct_blocks) { | |
| 2445 // Append, in descending order from the largest ACKed packet, a series of | |
| 2446 // ACK blocks that represents the successfully acknoweldged packets. Each | |
| 2447 // appended gap/block length represents a descending delta from the | |
| 2448 // previous block. i.e.: | |
| 2449 // |--- length ---|--- gap ---|--- length ---|--- gap ---|--- largest ---| | |
| 2450 // For gaps larger than can be represented by a single encoded gap, a 0 | |
| 2451 // length gap of the maximum is used, i.e.: | |
| 2452 // |--- length ---|--- gap ---|- 0 -|--- gap ---|--- largest ---| | |
| 2453 auto itr = frame.packets.rbegin_intervals(); | |
| 2454 QuicPacketNumber previous_start = itr->min(); | |
| 2455 ++itr; | |
| 2456 | |
| 2457 for (; itr != frame.packets.rend_intervals() && | |
| 2458 num_ack_blocks_written < num_ack_blocks; | |
| 2459 previous_start = itr->min(), ++itr) { | |
| 2460 const auto& interval = *itr; | |
| 2461 const QuicPacketNumber total_gap = previous_start - interval.max(); | |
| 2462 const size_t num_encoded_gaps = | |
| 2463 (total_gap + numeric_limits<uint8_t>::max() - 1) / | |
| 2464 numeric_limits<uint8_t>::max(); | |
| 2465 DCHECK_GT(num_encoded_gaps, 0u); | |
| 2466 | |
| 2467 // Append empty ACK blocks because the gap is longer than a single gap. | |
| 2468 for (size_t i = 1; | |
| 2469 i < num_encoded_gaps && num_ack_blocks_written < num_ack_blocks; | |
| 2470 ++i) { | |
| 2471 if (!AppendAckBlock(numeric_limits<uint8_t>::max(), ack_block_length, | |
| 2472 0, writer)) { | |
| 2473 return false; | |
| 2474 } | |
| 2475 ++num_ack_blocks_written; | |
| 2476 } | |
| 2477 if (num_ack_blocks_written >= num_ack_blocks) { | |
| 2478 if (PREDICT_FALSE(num_ack_blocks_written != num_ack_blocks)) { | |
| 2479 QUIC_BUG << "Wrote " << num_ack_blocks_written | |
| 2480 << ", expected to write " << num_ack_blocks; | |
| 2481 } | |
| 2482 break; | |
| 2483 } | |
| 2484 | |
| 2485 const uint8_t last_gap = | |
| 2486 total_gap - (num_encoded_gaps - 1) * numeric_limits<uint8_t>::max(); | |
| 2487 // Append the final ACK block with a non-empty size. | |
| 2488 if (!AppendAckBlock(last_gap, ack_block_length, interval.Length(), | |
| 2489 writer)) { | |
| 2490 return false; | |
| 2491 } | |
| 2492 ++num_ack_blocks_written; | |
| 2493 } | |
| 2494 } else { | |
| 2495 DCHECK_EQ(new_ack_info.num_ack_blocks, new_ack_info.ack_blocks.size()); | |
| 2496 vector<AckBlock>::const_reverse_iterator iter = | |
| 2497 new_ack_info.ack_blocks.rbegin(); | |
| 2498 for (; iter != new_ack_info.ack_blocks.rend(); ++iter) { | |
| 2499 if (!AppendPacketSequenceNumber(PACKET_1BYTE_PACKET_NUMBER, iter->gap, | |
| 2500 writer)) { | |
| 2501 return false; | |
| 2502 } | |
| 2503 if (!AppendPacketSequenceNumber(ack_block_length, iter->length, | |
| 2504 writer)) { | |
| 2505 return false; | |
| 2506 } | |
| 2507 if (++num_ack_blocks_written == num_ack_blocks) { | |
| 2508 break; | |
| 2509 } | |
| 2510 } | |
| 2511 } | |
| 2512 DCHECK_EQ(num_ack_blocks, num_ack_blocks_written); | |
| 2513 } | |
| 2514 | |
| 2515 // Timestamps. | |
| 2516 // If we don't have enough available space to append all the timestamps, don't | |
| 2517 // append any of them. | |
| 2518 if (writer->capacity() - writer->length() >= | |
| 2519 GetAckFrameTimeStampSize(frame)) { | |
| 2520 if (!AppendTimestampToAckFrame(frame, writer)) { | |
| 2521 return false; | |
| 2522 } | |
| 2523 } else { | |
| 2524 uint8_t num_received_packets = 0; | |
| 2525 if (!writer->WriteBytes(&num_received_packets, 1)) { | |
| 2526 return false; | |
| 2527 } | |
| 2528 } | |
| 2529 | |
| 2530 return true; | |
| 2531 } | |
| 2532 | |
| 2533 bool QuicFramer::AppendTimestampToAckFrame(const QuicAckFrame& frame, | |
| 2534 QuicDataWriter* writer) { | |
| 2535 DCHECK_GE(numeric_limits<uint8_t>::max(), frame.received_packet_times.size()); | |
| 2536 // num_received_packets is only 1 byte. | |
| 2537 if (frame.received_packet_times.size() > numeric_limits<uint8_t>::max()) { | |
| 2538 return false; | |
| 2539 } | |
| 2540 | |
| 2541 uint8_t num_received_packets = frame.received_packet_times.size(); | |
| 2542 if (!writer->WriteBytes(&num_received_packets, 1)) { | |
| 2543 return false; | |
| 2544 } | |
| 2545 if (num_received_packets == 0) { | |
| 2546 return true; | |
| 2547 } | |
| 2548 | |
| 2549 PacketTimeVector::const_iterator it = frame.received_packet_times.begin(); | |
| 2550 QuicPacketNumber packet_number = it->first; | |
| 2551 QuicPacketNumber delta_from_largest_observed = | |
| 2552 frame.largest_observed - packet_number; | |
| 2553 | |
| 2554 DCHECK_GE(numeric_limits<uint8_t>::max(), delta_from_largest_observed); | |
| 2555 if (delta_from_largest_observed > numeric_limits<uint8_t>::max()) { | |
| 2556 return false; | |
| 2557 } | |
| 2558 | |
| 2559 if (!writer->WriteUInt8(delta_from_largest_observed & | |
| 2560 k1ByteSequenceNumberMask)) { | |
| 2561 return false; | |
| 2562 } | |
| 2563 | |
| 2564 // Use the lowest 4 bytes of the time delta from the creation_time_. | |
| 2565 const uint64_t time_epoch_delta_us = UINT64_C(1) << 32; | |
| 2566 uint32_t time_delta_us = | |
| 2567 static_cast<uint32_t>((it->second - creation_time_).ToMicroseconds() & | |
| 2568 (time_epoch_delta_us - 1)); | |
| 2569 if (!writer->WriteBytes(&time_delta_us, sizeof(time_delta_us))) { | |
| 2570 return false; | |
| 2571 } | |
| 2572 | |
| 2573 QuicTime prev_time = it->second; | |
| 2574 | |
| 2575 for (++it; it != frame.received_packet_times.end(); ++it) { | |
| 2576 packet_number = it->first; | |
| 2577 delta_from_largest_observed = frame.largest_observed - packet_number; | |
| 2578 | |
| 2579 if (delta_from_largest_observed > numeric_limits<uint8_t>::max()) { | |
| 2580 return false; | |
| 2581 } | |
| 2582 | |
| 2583 if (!writer->WriteUInt8(delta_from_largest_observed & | |
| 2584 k1ByteSequenceNumberMask)) { | |
| 2585 return false; | |
| 2586 } | |
| 2587 | |
| 2588 uint64_t frame_time_delta_us = (it->second - prev_time).ToMicroseconds(); | |
| 2589 prev_time = it->second; | |
| 2590 if (!writer->WriteUFloat16(frame_time_delta_us)) { | |
| 2591 return false; | |
| 2592 } | |
| 2593 } | |
| 2594 return true; | |
| 2595 } | |
| 2596 | |
| 2597 bool QuicFramer::AppendStopWaitingFrame(const QuicPacketHeader& header, | |
| 2598 const QuicStopWaitingFrame& frame, | |
| 2599 QuicDataWriter* writer) { | |
| 2600 DCHECK_GE(header.packet_number, frame.least_unacked); | |
| 2601 const QuicPacketNumber least_unacked_delta = | |
| 2602 header.packet_number - frame.least_unacked; | |
| 2603 const QuicPacketNumber length_shift = | |
| 2604 header.public_header.packet_number_length * 8; | |
| 2605 if (quic_version_ <= QUIC_VERSION_33) { | |
| 2606 if (!writer->WriteUInt8(frame.entropy_hash)) { | |
| 2607 QUIC_BUG << " hash failed"; | |
| 2608 return false; | |
| 2609 } | |
| 2610 } | |
| 2611 | |
| 2612 if (least_unacked_delta >> length_shift > 0) { | |
| 2613 QUIC_BUG << "packet_number_length " | |
| 2614 << header.public_header.packet_number_length | |
| 2615 << " is too small for least_unacked_delta: " << least_unacked_delta | |
| 2616 << " packet_number:" << header.packet_number | |
| 2617 << " least_unacked:" << frame.least_unacked | |
| 2618 << " version:" << quic_version_; | |
| 2619 return false; | |
| 2620 } | |
| 2621 if (!AppendPacketSequenceNumber(header.public_header.packet_number_length, | |
| 2622 least_unacked_delta, writer)) { | |
| 2623 QUIC_BUG << " seq failed: " << header.public_header.packet_number_length; | |
| 2624 return false; | |
| 2625 } | |
| 2626 | |
| 2627 return true; | |
| 2628 } | |
| 2629 | |
| 2630 bool QuicFramer::AppendRstStreamFrame(const QuicRstStreamFrame& frame, | |
| 2631 QuicDataWriter* writer) { | |
| 2632 if (!writer->WriteUInt32(frame.stream_id)) { | |
| 2633 return false; | |
| 2634 } | |
| 2635 | |
| 2636 if (!writer->WriteUInt64(frame.byte_offset)) { | |
| 2637 return false; | |
| 2638 } | |
| 2639 | |
| 2640 uint32_t error_code = static_cast<uint32_t>(frame.error_code); | |
| 2641 if (!writer->WriteUInt32(error_code)) { | |
| 2642 return false; | |
| 2643 } | |
| 2644 | |
| 2645 return true; | |
| 2646 } | |
| 2647 | |
| 2648 bool QuicFramer::AppendConnectionCloseFrame( | |
| 2649 const QuicConnectionCloseFrame& frame, | |
| 2650 QuicDataWriter* writer) { | |
| 2651 uint32_t error_code = static_cast<uint32_t>(frame.error_code); | |
| 2652 if (!writer->WriteUInt32(error_code)) { | |
| 2653 return false; | |
| 2654 } | |
| 2655 if (!writer->WriteStringPiece16(frame.error_details)) { | |
| 2656 return false; | |
| 2657 } | |
| 2658 return true; | |
| 2659 } | |
| 2660 | |
| 2661 bool QuicFramer::AppendGoAwayFrame(const QuicGoAwayFrame& frame, | |
| 2662 QuicDataWriter* writer) { | |
| 2663 uint32_t error_code = static_cast<uint32_t>(frame.error_code); | |
| 2664 if (!writer->WriteUInt32(error_code)) { | |
| 2665 return false; | |
| 2666 } | |
| 2667 uint32_t stream_id = static_cast<uint32_t>(frame.last_good_stream_id); | |
| 2668 if (!writer->WriteUInt32(stream_id)) { | |
| 2669 return false; | |
| 2670 } | |
| 2671 if (!writer->WriteStringPiece16(frame.reason_phrase)) { | |
| 2672 return false; | |
| 2673 } | |
| 2674 return true; | |
| 2675 } | |
| 2676 | |
| 2677 bool QuicFramer::AppendWindowUpdateFrame(const QuicWindowUpdateFrame& frame, | |
| 2678 QuicDataWriter* writer) { | |
| 2679 uint32_t stream_id = static_cast<uint32_t>(frame.stream_id); | |
| 2680 if (!writer->WriteUInt32(stream_id)) { | |
| 2681 return false; | |
| 2682 } | |
| 2683 if (!writer->WriteUInt64(frame.byte_offset)) { | |
| 2684 return false; | |
| 2685 } | |
| 2686 return true; | |
| 2687 } | |
| 2688 | |
| 2689 bool QuicFramer::AppendBlockedFrame(const QuicBlockedFrame& frame, | |
| 2690 QuicDataWriter* writer) { | |
| 2691 uint32_t stream_id = static_cast<uint32_t>(frame.stream_id); | |
| 2692 if (!writer->WriteUInt32(stream_id)) { | |
| 2693 return false; | |
| 2694 } | |
| 2695 return true; | |
| 2696 } | |
| 2697 | |
| 2698 bool QuicFramer::AppendPathCloseFrame(const QuicPathCloseFrame& frame, | |
| 2699 QuicDataWriter* writer) { | |
| 2700 uint8_t path_id = static_cast<uint8_t>(frame.path_id); | |
| 2701 if (!writer->WriteUInt8(path_id)) { | |
| 2702 return false; | |
| 2703 } | |
| 2704 return true; | |
| 2705 } | |
| 2706 | |
| 2707 bool QuicFramer::RaiseError(QuicErrorCode error) { | |
| 2708 DVLOG(1) << "Error: " << QuicUtils::ErrorToString(error) | |
| 2709 << " detail: " << detailed_error_; | |
| 2710 set_error(error); | |
| 2711 visitor_->OnError(this); | |
| 2712 return false; | |
| 2713 } | |
| 2714 | |
| 2715 } // namespace net | |
| OLD | NEW |