| 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/quic_framer.h" | 5 #include "net/quic/quic_framer.h" |
| 6 | 6 |
| 7 #include "base/containers/hash_tables.h" | 7 #include "base/containers/hash_tables.h" |
| 8 #include "net/quic/crypto/crypto_framer.h" |
| 9 #include "net/quic/crypto/crypto_handshake_message.h" |
| 8 #include "net/quic/crypto/quic_decrypter.h" | 10 #include "net/quic/crypto/quic_decrypter.h" |
| 9 #include "net/quic/crypto/quic_encrypter.h" | 11 #include "net/quic/crypto/quic_encrypter.h" |
| 10 #include "net/quic/quic_data_reader.h" | 12 #include "net/quic/quic_data_reader.h" |
| 11 #include "net/quic/quic_data_writer.h" | 13 #include "net/quic/quic_data_writer.h" |
| 14 #include "net/quic/quic_socket_address_coder.h" |
| 12 | 15 |
| 13 using base::StringPiece; | 16 using base::StringPiece; |
| 14 using std::make_pair; | 17 using std::make_pair; |
| 15 using std::map; | 18 using std::map; |
| 16 using std::max; | 19 using std::max; |
| 17 using std::min; | 20 using std::min; |
| 18 using std::numeric_limits; | 21 using std::numeric_limits; |
| 19 using std::string; | 22 using std::string; |
| 20 | 23 |
| 21 bool FLAGS_quic_allow_oversized_packets_for_test = false; | 24 bool FLAGS_quic_allow_oversized_packets_for_test = false; |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 header.public_header.guid_length, | 425 header.public_header.guid_length, |
| 423 header.public_header.version_flag, | 426 header.public_header.version_flag, |
| 424 header.public_header.sequence_number_length), | 427 header.public_header.sequence_number_length), |
| 425 GetPacketEntropyHash(header), NULL); | 428 GetPacketEntropyHash(header), NULL); |
| 426 } | 429 } |
| 427 | 430 |
| 428 // static | 431 // static |
| 429 QuicEncryptedPacket* QuicFramer::BuildPublicResetPacket( | 432 QuicEncryptedPacket* QuicFramer::BuildPublicResetPacket( |
| 430 const QuicPublicResetPacket& packet) { | 433 const QuicPublicResetPacket& packet) { |
| 431 DCHECK(packet.public_header.reset_flag); | 434 DCHECK(packet.public_header.reset_flag); |
| 432 size_t len = GetPublicResetPacketSize(); | 435 |
| 436 CryptoHandshakeMessage reset; |
| 437 reset.set_tag(kPRST); |
| 438 reset.SetValue(kRNON, packet.nonce_proof); |
| 439 reset.SetValue(kRSEQ, packet.rejected_sequence_number); |
| 440 if (!packet.client_address.address().empty()) { |
| 441 // packet.client_address is non-empty. |
| 442 QuicSocketAddressCoder address_coder(packet.client_address); |
| 443 string serialized_address = address_coder.Encode(); |
| 444 if (serialized_address.empty()) { |
| 445 return NULL; |
| 446 } |
| 447 reset.SetStringPiece(kCADR, serialized_address); |
| 448 } |
| 449 const QuicData& reset_serialized = reset.GetSerialized(); |
| 450 |
| 451 size_t len = kPublicFlagsSize + PACKET_8BYTE_GUID + reset_serialized.length(); |
| 433 QuicDataWriter writer(len); | 452 QuicDataWriter writer(len); |
| 434 | 453 |
| 435 uint8 flags = static_cast<uint8>(PACKET_PUBLIC_FLAGS_RST | | 454 uint8 flags = static_cast<uint8>(PACKET_PUBLIC_FLAGS_RST | |
| 436 PACKET_PUBLIC_FLAGS_8BYTE_GUID | | 455 PACKET_PUBLIC_FLAGS_8BYTE_GUID); |
| 437 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE); | |
| 438 if (!writer.WriteUInt8(flags)) { | 456 if (!writer.WriteUInt8(flags)) { |
| 439 return NULL; | 457 return NULL; |
| 440 } | 458 } |
| 441 | 459 |
| 442 if (!writer.WriteUInt64(packet.public_header.guid)) { | 460 if (!writer.WriteUInt64(packet.public_header.guid)) { |
| 443 return NULL; | 461 return NULL; |
| 444 } | 462 } |
| 445 | 463 |
| 446 if (!writer.WriteUInt64(packet.nonce_proof)) { | 464 if (!writer.WriteBytes(reset_serialized.data(), reset_serialized.length())) { |
| 447 return NULL; | |
| 448 } | |
| 449 | |
| 450 if (!AppendPacketSequenceNumber(PACKET_6BYTE_SEQUENCE_NUMBER, | |
| 451 packet.rejected_sequence_number, | |
| 452 &writer)) { | |
| 453 return NULL; | 465 return NULL; |
| 454 } | 466 } |
| 455 | 467 |
| 456 return new QuicEncryptedPacket(writer.take(), len, true); | 468 return new QuicEncryptedPacket(writer.take(), len, true); |
| 457 } | 469 } |
| 458 | 470 |
| 459 QuicEncryptedPacket* QuicFramer::BuildVersionNegotiationPacket( | 471 QuicEncryptedPacket* QuicFramer::BuildVersionNegotiationPacket( |
| 460 const QuicPacketPublicHeader& header, | 472 const QuicPacketPublicHeader& header, |
| 461 const QuicVersionVector& supported_versions) { | 473 const QuicVersionVector& supported_versions) { |
| 462 DCHECK(header.version_flag); | 474 DCHECK(header.version_flag); |
| 463 size_t len = GetVersionNegotiationPacketSize(supported_versions.size()); | 475 size_t len = GetVersionNegotiationPacketSize(supported_versions.size()); |
| 464 QuicDataWriter writer(len); | 476 QuicDataWriter writer(len); |
| 465 | 477 |
| 466 uint8 flags = static_cast<uint8>(PACKET_PUBLIC_FLAGS_VERSION | | 478 uint8 flags = static_cast<uint8>(PACKET_PUBLIC_FLAGS_VERSION | |
| 467 PACKET_PUBLIC_FLAGS_8BYTE_GUID | | 479 PACKET_PUBLIC_FLAGS_8BYTE_GUID); |
| 468 PACKET_PUBLIC_FLAGS_6BYTE_SEQUENCE); | |
| 469 if (!writer.WriteUInt8(flags)) { | 480 if (!writer.WriteUInt8(flags)) { |
| 470 return NULL; | 481 return NULL; |
| 471 } | 482 } |
| 472 | 483 |
| 473 if (!writer.WriteUInt64(header.guid)) { | 484 if (!writer.WriteUInt64(header.guid)) { |
| 474 return NULL; | 485 return NULL; |
| 475 } | 486 } |
| 476 | 487 |
| 477 for (size_t i = 0; i < supported_versions.size(); ++i) { | 488 for (size_t i = 0; i < supported_versions.size(); ++i) { |
| 478 if (!writer.WriteUInt32(QuicVersionToQuicTag(supported_versions[i]))) { | 489 if (!writer.WriteUInt32(QuicVersionToQuicTag(supported_versions[i]))) { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 visitor_->OnFecData(fec_data); | 589 visitor_->OnFecData(fec_data); |
| 579 } | 590 } |
| 580 | 591 |
| 581 visitor_->OnPacketComplete(); | 592 visitor_->OnPacketComplete(); |
| 582 return true; | 593 return true; |
| 583 } | 594 } |
| 584 | 595 |
| 585 bool QuicFramer::ProcessPublicResetPacket( | 596 bool QuicFramer::ProcessPublicResetPacket( |
| 586 const QuicPacketPublicHeader& public_header) { | 597 const QuicPacketPublicHeader& public_header) { |
| 587 QuicPublicResetPacket packet(public_header); | 598 QuicPublicResetPacket packet(public_header); |
| 588 if (!reader_->ReadUInt64(&packet.nonce_proof)) { | 599 |
| 600 if (reader_->BytesRemaining() <= |
| 601 kPublicResetNonceSize + PACKET_6BYTE_SEQUENCE_NUMBER) { |
| 602 // An old-style public reset packet. |
| 603 // TODO(wtc): remove this when we drop support for QUIC_VERSION_13. |
| 604 if (!reader_->ReadUInt64(&packet.nonce_proof)) { |
| 605 set_detailed_error("Unable to read nonce proof."); |
| 606 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 607 } |
| 608 |
| 609 if (!reader_->ReadUInt48(&packet.rejected_sequence_number)) { |
| 610 set_detailed_error("Unable to read rejected sequence number."); |
| 611 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 612 } |
| 613 |
| 614 visitor_->OnPublicResetPacket(packet); |
| 615 return true; |
| 616 } |
| 617 |
| 618 scoped_ptr<CryptoHandshakeMessage> reset( |
| 619 CryptoFramer::ParseMessage(reader_->ReadRemainingPayload())); |
| 620 if (!reset.get()) { |
| 621 set_detailed_error("Unable to read reset message."); |
| 622 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 623 } |
| 624 if (reset->tag() != kPRST) { |
| 625 set_detailed_error("Incorrect message tag."); |
| 626 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 627 } |
| 628 |
| 629 if (reset->GetUint64(kRNON, &packet.nonce_proof) != QUIC_NO_ERROR) { |
| 589 set_detailed_error("Unable to read nonce proof."); | 630 set_detailed_error("Unable to read nonce proof."); |
| 590 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); | 631 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 591 } | 632 } |
| 592 // TODO(satyamshekhar): validate nonce to protect against DoS. | 633 // TODO(satyamshekhar): validate nonce to protect against DoS. |
| 593 | 634 |
| 594 if (!reader_->ReadUInt48(&packet.rejected_sequence_number)) { | 635 if (reset->GetUint64(kRSEQ, &packet.rejected_sequence_number) != |
| 636 QUIC_NO_ERROR) { |
| 595 set_detailed_error("Unable to read rejected sequence number."); | 637 set_detailed_error("Unable to read rejected sequence number."); |
| 596 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); | 638 return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 597 } | 639 } |
| 640 |
| 641 StringPiece address; |
| 642 if (reset->GetStringPiece(kCADR, &address)) { |
| 643 QuicSocketAddressCoder address_coder; |
| 644 if (address_coder.Decode(address.data(), address.length())) { |
| 645 packet.client_address = IPEndPoint(address_coder.ip(), |
| 646 address_coder.port()); |
| 647 } |
| 648 } |
| 649 |
| 598 visitor_->OnPublicResetPacket(packet); | 650 visitor_->OnPublicResetPacket(packet); |
| 599 return true; | 651 return true; |
| 600 } | 652 } |
| 601 | 653 |
| 602 bool QuicFramer::ProcessRevivedPacket(QuicPacketHeader* header, | 654 bool QuicFramer::ProcessRevivedPacket(QuicPacketHeader* header, |
| 603 StringPiece payload) { | 655 StringPiece payload) { |
| 604 DCHECK(!reader_.get()); | 656 DCHECK(!reader_.get()); |
| 605 | 657 |
| 606 visitor_->OnRevivedPacket(); | 658 visitor_->OnRevivedPacket(); |
| 607 | 659 |
| (...skipping 1444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2052 | 2104 |
| 2053 bool QuicFramer::RaiseError(QuicErrorCode error) { | 2105 bool QuicFramer::RaiseError(QuicErrorCode error) { |
| 2054 DVLOG(1) << "Error detail: " << detailed_error_; | 2106 DVLOG(1) << "Error detail: " << detailed_error_; |
| 2055 set_error(error); | 2107 set_error(error); |
| 2056 visitor_->OnError(this); | 2108 visitor_->OnError(this); |
| 2057 reader_.reset(NULL); | 2109 reader_.reset(NULL); |
| 2058 return false; | 2110 return false; |
| 2059 } | 2111 } |
| 2060 | 2112 |
| 2061 } // namespace net | 2113 } // namespace net |
| OLD | NEW |