| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/core/quic_framer.h" | 5 #include "net/quic/core/quic_framer.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 : max_block_length(0), first_block_length(0), num_ack_blocks(0) {} | 306 : max_block_length(0), first_block_length(0), num_ack_blocks(0) {} |
| 307 | 307 |
| 308 QuicFramer::AckFrameInfo::AckFrameInfo(const AckFrameInfo& other) = default; | 308 QuicFramer::AckFrameInfo::AckFrameInfo(const AckFrameInfo& other) = default; |
| 309 | 309 |
| 310 QuicFramer::AckFrameInfo::~AckFrameInfo() {} | 310 QuicFramer::AckFrameInfo::~AckFrameInfo() {} |
| 311 | 311 |
| 312 size_t QuicFramer::BuildDataPacket(const QuicPacketHeader& header, | 312 size_t QuicFramer::BuildDataPacket(const QuicPacketHeader& header, |
| 313 const QuicFrames& frames, | 313 const QuicFrames& frames, |
| 314 char* buffer, | 314 char* buffer, |
| 315 size_t packet_length) { | 315 size_t packet_length) { |
| 316 QuicDataWriter writer(packet_length, buffer, perspective_, HOST_BYTE_ORDER); | 316 QuicDataWriter writer(packet_length, buffer, perspective_, endianness()); |
| 317 if (!AppendPacketHeader(header, &writer)) { | 317 if (!AppendPacketHeader(header, &writer)) { |
| 318 QUIC_BUG << "AppendPacketHeader failed"; | 318 QUIC_BUG << "AppendPacketHeader failed"; |
| 319 return 0; | 319 return 0; |
| 320 } | 320 } |
| 321 | 321 |
| 322 size_t i = 0; | 322 size_t i = 0; |
| 323 for (const QuicFrame& frame : frames) { | 323 for (const QuicFrame& frame : frames) { |
| 324 // Determine if we should write stream frame length in header. | 324 // Determine if we should write stream frame length in header. |
| 325 const bool no_stream_frame_length = i == frames.size() - 1; | 325 const bool no_stream_frame_length = i == frames.size() - 1; |
| 326 if (!AppendTypeByte(frame, no_stream_frame_length, &writer)) { | 326 if (!AppendTypeByte(frame, no_stream_frame_length, &writer)) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 return nullptr; | 420 return nullptr; |
| 421 } | 421 } |
| 422 reset.SetStringPiece(kCADR, serialized_address); | 422 reset.SetStringPiece(kCADR, serialized_address); |
| 423 } | 423 } |
| 424 const QuicData& reset_serialized = | 424 const QuicData& reset_serialized = |
| 425 reset.GetSerialized(Perspective::IS_SERVER); | 425 reset.GetSerialized(Perspective::IS_SERVER); |
| 426 | 426 |
| 427 size_t len = | 427 size_t len = |
| 428 kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID + reset_serialized.length(); | 428 kPublicFlagsSize + PACKET_8BYTE_CONNECTION_ID + reset_serialized.length(); |
| 429 std::unique_ptr<char[]> buffer(new char[len]); | 429 std::unique_ptr<char[]> buffer(new char[len]); |
| 430 // Endianness is not a concern here, as writer is not going to write integers |
| 431 // or floating numbers. |
| 430 QuicDataWriter writer(len, buffer.get(), Perspective::IS_SERVER, | 432 QuicDataWriter writer(len, buffer.get(), Perspective::IS_SERVER, |
| 431 HOST_BYTE_ORDER); | 433 NETWORK_BYTE_ORDER); |
| 432 | 434 |
| 433 uint8_t flags = static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_RST | | 435 uint8_t flags = static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_RST | |
| 434 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID); | 436 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID); |
| 435 if (FLAGS_quic_reloadable_flag_quic_use_old_public_reset_packets) { | 437 if (FLAGS_quic_reloadable_flag_quic_use_old_public_reset_packets) { |
| 436 // TODO(rch): Remove this QUIC_VERSION_32 is retired. | 438 // TODO(rch): Remove this QUIC_VERSION_32 is retired. |
| 437 flags |= static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); | 439 flags |= static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); |
| 438 } | 440 } |
| 439 if (!writer.WriteUInt8(flags)) { | 441 if (!writer.WriteUInt8(flags)) { |
| 440 return nullptr; | 442 return nullptr; |
| 441 } | 443 } |
| 442 | 444 |
| 443 if (!writer.WriteConnectionId(packet.public_header.connection_id)) { | 445 if (!writer.WriteConnectionId(packet.public_header.connection_id)) { |
| 444 return nullptr; | 446 return nullptr; |
| 445 } | 447 } |
| 446 | 448 |
| 447 if (!writer.WriteBytes(reset_serialized.data(), reset_serialized.length())) { | 449 if (!writer.WriteBytes(reset_serialized.data(), reset_serialized.length())) { |
| 448 return nullptr; | 450 return nullptr; |
| 449 } | 451 } |
| 450 | 452 |
| 451 return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); | 453 return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); |
| 452 } | 454 } |
| 453 | 455 |
| 454 // static | 456 // static |
| 455 std::unique_ptr<QuicEncryptedPacket> QuicFramer::BuildVersionNegotiationPacket( | 457 std::unique_ptr<QuicEncryptedPacket> QuicFramer::BuildVersionNegotiationPacket( |
| 456 QuicConnectionId connection_id, | 458 QuicConnectionId connection_id, |
| 457 const QuicVersionVector& versions) { | 459 const QuicVersionVector& versions) { |
| 458 DCHECK(!versions.empty()); | 460 DCHECK(!versions.empty()); |
| 459 size_t len = GetVersionNegotiationPacketSize(versions.size()); | 461 size_t len = GetVersionNegotiationPacketSize(versions.size()); |
| 460 std::unique_ptr<char[]> buffer(new char[len]); | 462 std::unique_ptr<char[]> buffer(new char[len]); |
| 463 // Endianness is not a concern here, version negotiation packet does not have |
| 464 // integers or floating numbers. |
| 461 QuicDataWriter writer(len, buffer.get(), Perspective::IS_SERVER, | 465 QuicDataWriter writer(len, buffer.get(), Perspective::IS_SERVER, |
| 462 HOST_BYTE_ORDER); | 466 NETWORK_BYTE_ORDER); |
| 463 | 467 |
| 464 uint8_t flags = static_cast<uint8_t>( | 468 uint8_t flags = static_cast<uint8_t>( |
| 465 PACKET_PUBLIC_FLAGS_VERSION | PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID | | 469 PACKET_PUBLIC_FLAGS_VERSION | PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID | |
| 466 // TODO(rch): Remove this QUIC_VERSION_32 is retired. | 470 // TODO(rch): Remove this QUIC_VERSION_32 is retired. |
| 467 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); | 471 PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); |
| 468 if (!writer.WriteUInt8(flags)) { | 472 if (!writer.WriteUInt8(flags)) { |
| 469 return nullptr; | 473 return nullptr; |
| 470 } | 474 } |
| 471 | 475 |
| 472 if (!writer.WriteConnectionId(connection_id)) { | 476 if (!writer.WriteConnectionId(connection_id)) { |
| 473 return nullptr; | 477 return nullptr; |
| 474 } | 478 } |
| 475 | 479 |
| 476 for (QuicVersion version : versions) { | 480 for (QuicVersion version : versions) { |
| 477 if (!writer.WriteTag(QuicVersionToQuicTag(version))) { | 481 if (!writer.WriteTag(QuicVersionToQuicTag(version))) { |
| 478 return nullptr; | 482 return nullptr; |
| 479 } | 483 } |
| 480 } | 484 } |
| 481 | 485 |
| 482 return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); | 486 return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); |
| 483 } | 487 } |
| 484 | 488 |
| 485 bool QuicFramer::ProcessPacket(const QuicEncryptedPacket& packet) { | 489 bool QuicFramer::ProcessPacket(const QuicEncryptedPacket& packet) { |
| 486 QuicDataReader reader(packet.data(), packet.length(), perspective_, | 490 QuicDataReader reader(packet.data(), packet.length(), perspective_, |
| 487 HOST_BYTE_ORDER); | 491 endianness()); |
| 488 | 492 |
| 489 visitor_->OnPacket(); | 493 visitor_->OnPacket(); |
| 490 | 494 |
| 491 // First parse the public header. | 495 // First parse the public header. |
| 492 QuicPacketPublicHeader public_header; | 496 QuicPacketPublicHeader public_header; |
| 493 if (!ProcessPublicHeader(&reader, &public_header)) { | 497 if (!ProcessPublicHeader(&reader, &public_header)) { |
| 494 DCHECK_NE("", detailed_error_); | 498 DCHECK_NE("", detailed_error_); |
| 495 QUIC_DVLOG(1) << ENDPOINT << "Unable to process public header. Error: " | 499 QUIC_DVLOG(1) << ENDPOINT << "Unable to process public header. Error: " |
| 496 << detailed_error_; | 500 << detailed_error_; |
| 497 DCHECK_NE("", detailed_error_); | 501 DCHECK_NE("", detailed_error_); |
| 498 return RaiseError(QUIC_INVALID_PACKET_HEADER); | 502 return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 499 } | 503 } |
| 500 | 504 |
| 501 if (!visitor_->OnUnauthenticatedPublicHeader(public_header)) { | 505 if (!visitor_->OnUnauthenticatedPublicHeader(public_header)) { |
| 502 // The visitor suppresses further processing of the packet. | 506 // The visitor suppresses further processing of the packet. |
| 503 return true; | 507 return true; |
| 504 } | 508 } |
| 505 | 509 |
| 506 if (perspective_ == Perspective::IS_SERVER && public_header.version_flag && | 510 if (perspective_ == Perspective::IS_SERVER && public_header.version_flag && |
| 507 public_header.versions[0] != quic_version_) { | 511 public_header.versions[0] != quic_version_) { |
| 508 if (!visitor_->OnProtocolVersionMismatch(public_header.versions[0])) { | 512 if (!visitor_->OnProtocolVersionMismatch(public_header.versions[0])) { |
| 509 return true; | 513 return true; |
| 510 } | 514 } |
| 511 } | 515 } |
| 512 | 516 |
| 517 // framer's version may change, reset reader's endianness. |
| 518 reader.set_endianness(endianness()); |
| 519 |
| 513 bool rv; | 520 bool rv; |
| 514 if (perspective_ == Perspective::IS_CLIENT && public_header.version_flag) { | 521 if (perspective_ == Perspective::IS_CLIENT && public_header.version_flag) { |
| 515 rv = ProcessVersionNegotiationPacket(&reader, &public_header); | 522 rv = ProcessVersionNegotiationPacket(&reader, &public_header); |
| 516 } else if (public_header.reset_flag) { | 523 } else if (public_header.reset_flag) { |
| 517 rv = ProcessPublicResetPacket(&reader, public_header); | 524 rv = ProcessPublicResetPacket(&reader, public_header); |
| 518 } else if (packet.length() <= kMaxPacketSize) { | 525 } else if (packet.length() <= kMaxPacketSize) { |
| 519 // The optimized decryption algorithm implementations run faster when | 526 // The optimized decryption algorithm implementations run faster when |
| 520 // operating on aligned memory. | 527 // operating on aligned memory. |
| 521 QUIC_CACHELINE_ALIGNED char buffer[kMaxPacketSize]; | 528 QUIC_CACHELINE_ALIGNED char buffer[kMaxPacketSize]; |
| 522 rv = ProcessDataPacket(&reader, public_header, packet, buffer, | 529 rv = ProcessDataPacket(&reader, public_header, packet, buffer, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 } | 573 } |
| 567 | 574 |
| 568 size_t decrypted_length = 0; | 575 size_t decrypted_length = 0; |
| 569 if (!DecryptPayload(encrypted_reader, header, packet, decrypted_buffer, | 576 if (!DecryptPayload(encrypted_reader, header, packet, decrypted_buffer, |
| 570 buffer_length, &decrypted_length)) { | 577 buffer_length, &decrypted_length)) { |
| 571 set_detailed_error("Unable to decrypt payload."); | 578 set_detailed_error("Unable to decrypt payload."); |
| 572 return RaiseError(QUIC_DECRYPTION_FAILURE); | 579 return RaiseError(QUIC_DECRYPTION_FAILURE); |
| 573 } | 580 } |
| 574 | 581 |
| 575 QuicDataReader reader(decrypted_buffer, decrypted_length, perspective_, | 582 QuicDataReader reader(decrypted_buffer, decrypted_length, perspective_, |
| 576 HOST_BYTE_ORDER); | 583 endianness()); |
| 577 | 584 |
| 578 // Set the last packet number after we have decrypted the packet | 585 // Set the last packet number after we have decrypted the packet |
| 579 // so we are confident is not attacker controlled. | 586 // so we are confident is not attacker controlled. |
| 580 SetLastPacketNumber(header); | 587 SetLastPacketNumber(header); |
| 581 | 588 |
| 582 if (!visitor_->OnPacketHeader(header)) { | 589 if (!visitor_->OnPacketHeader(header)) { |
| 583 // The visitor suppresses further processing of the packet. | 590 // The visitor suppresses further processing of the packet. |
| 584 return true; | 591 return true; |
| 585 } | 592 } |
| 586 | 593 |
| (...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2154 } | 2161 } |
| 2155 | 2162 |
| 2156 bool QuicFramer::RaiseError(QuicErrorCode error) { | 2163 bool QuicFramer::RaiseError(QuicErrorCode error) { |
| 2157 QUIC_DLOG(INFO) << ENDPOINT << "Error: " << QuicErrorCodeToString(error) | 2164 QUIC_DLOG(INFO) << ENDPOINT << "Error: " << QuicErrorCodeToString(error) |
| 2158 << " detail: " << detailed_error_; | 2165 << " detail: " << detailed_error_; |
| 2159 set_error(error); | 2166 set_error(error); |
| 2160 visitor_->OnError(this); | 2167 visitor_->OnError(this); |
| 2161 return false; | 2168 return false; |
| 2162 } | 2169 } |
| 2163 | 2170 |
| 2171 Endianness QuicFramer::endianness() const { |
| 2172 return quic_version_ > QUIC_VERSION_38 ? NETWORK_BYTE_ORDER : HOST_BYTE_ORDER; |
| 2173 } |
| 2174 |
| 2164 } // namespace net | 2175 } // namespace net |
| OLD | NEW |