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 #ifndef QUICHE_QUIC_CORE_QUIC_PACKETS_H_ 6 #define QUICHE_QUIC_CORE_QUIC_PACKETS_H_ 7 8 #include <sys/types.h> 9 10 #include <cstddef> 11 #include <cstdint> 12 #include <memory> 13 #include <ostream> 14 #include <string> 15 #include <utility> 16 17 #include "absl/strings/string_view.h" 18 #include "quiche/quic/core/frames/quic_frame.h" 19 #include "quiche/quic/core/quic_ack_listener_interface.h" 20 #include "quiche/quic/core/quic_bandwidth.h" 21 #include "quiche/quic/core/quic_constants.h" 22 #include "quiche/quic/core/quic_error_codes.h" 23 #include "quiche/quic/core/quic_time.h" 24 #include "quiche/quic/core/quic_types.h" 25 #include "quiche/quic/core/quic_versions.h" 26 #include "quiche/quic/platform/api/quic_export.h" 27 #include "quiche/quic/platform/api/quic_socket_address.h" 28 29 namespace quic { 30 31 class QuicPacket; 32 struct QuicPacketHeader; 33 34 // Returns the destination connection ID of |header| when |perspective| is 35 // server, and the source connection ID when |perspective| is client. 36 QUIC_EXPORT_PRIVATE QuicConnectionId GetServerConnectionIdAsRecipient( 37 const QuicPacketHeader& header, Perspective perspective); 38 39 // Returns the destination connection ID of |header| when |perspective| is 40 // client, and the source connection ID when |perspective| is server. 41 QUIC_EXPORT_PRIVATE QuicConnectionId GetClientConnectionIdAsRecipient( 42 const QuicPacketHeader& header, Perspective perspective); 43 44 // Returns the destination connection ID of |header| when |perspective| is 45 // client, and the source connection ID when |perspective| is server. 46 QUIC_EXPORT_PRIVATE QuicConnectionId GetServerConnectionIdAsSender( 47 const QuicPacketHeader& header, Perspective perspective); 48 49 // Returns the destination connection ID included of |header| when |perspective| 50 // is client, and the source connection ID included when |perspective| is 51 // server. 52 QUIC_EXPORT_PRIVATE QuicConnectionIdIncluded 53 GetServerConnectionIdIncludedAsSender(const QuicPacketHeader& header, 54 Perspective perspective); 55 56 // Returns the destination connection ID of |header| when |perspective| is 57 // server, and the source connection ID when |perspective| is client. 58 QUIC_EXPORT_PRIVATE QuicConnectionId GetClientConnectionIdAsSender( 59 const QuicPacketHeader& header, Perspective perspective); 60 61 // Returns the destination connection ID included of |header| when |perspective| 62 // is server, and the source connection ID included when |perspective| is 63 // client. 64 QUIC_EXPORT_PRIVATE QuicConnectionIdIncluded 65 GetClientConnectionIdIncludedAsSender(const QuicPacketHeader& header, 66 Perspective perspective); 67 68 // Number of connection ID bytes that are actually included over the wire. 69 QUIC_EXPORT_PRIVATE uint8_t 70 GetIncludedConnectionIdLength(QuicConnectionId connection_id, 71 QuicConnectionIdIncluded connection_id_included); 72 73 // Number of destination connection ID bytes that are actually included over the 74 // wire for this particular header. 75 QUIC_EXPORT_PRIVATE uint8_t 76 GetIncludedDestinationConnectionIdLength(const QuicPacketHeader& header); 77 78 // Number of source connection ID bytes that are actually included over the 79 // wire for this particular header. 80 QUIC_EXPORT_PRIVATE uint8_t 81 GetIncludedSourceConnectionIdLength(const QuicPacketHeader& header); 82 83 // Size in bytes of the data packet header. 84 QUIC_EXPORT_PRIVATE size_t GetPacketHeaderSize(QuicTransportVersion version, 85 const QuicPacketHeader& header); 86 87 QUIC_EXPORT_PRIVATE size_t GetPacketHeaderSize( 88 QuicTransportVersion version, uint8_t destination_connection_id_length, 89 uint8_t source_connection_id_length, bool include_version, 90 bool include_diversification_nonce, 91 QuicPacketNumberLength packet_number_length, 92 quiche::QuicheVariableLengthIntegerLength retry_token_length_length, 93 QuicByteCount retry_token_length, 94 quiche::QuicheVariableLengthIntegerLength length_length); 95 96 // Index of the first byte in a QUIC packet of encrypted data. 97 QUIC_EXPORT_PRIVATE size_t GetStartOfEncryptedData( 98 QuicTransportVersion version, const QuicPacketHeader& header); 99 100 QUIC_EXPORT_PRIVATE size_t GetStartOfEncryptedData( 101 QuicTransportVersion version, uint8_t destination_connection_id_length, 102 uint8_t source_connection_id_length, bool include_version, 103 bool include_diversification_nonce, 104 QuicPacketNumberLength packet_number_length, 105 quiche::QuicheVariableLengthIntegerLength retry_token_length_length, 106 QuicByteCount retry_token_length, 107 quiche::QuicheVariableLengthIntegerLength length_length); 108 109 struct QUIC_EXPORT_PRIVATE QuicPacketHeader { 110 QuicPacketHeader(); 111 QuicPacketHeader(const QuicPacketHeader& other); 112 ~QuicPacketHeader(); 113 114 QuicPacketHeader& operator=(const QuicPacketHeader& other); 115 116 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<( 117 std::ostream& os, const QuicPacketHeader& header); 118 119 // Universal header. All QuicPacket headers will have a connection_id and 120 // public flags. 121 QuicConnectionId destination_connection_id; 122 QuicConnectionIdIncluded destination_connection_id_included; 123 QuicConnectionId source_connection_id; 124 QuicConnectionIdIncluded source_connection_id_included; 125 // This is only used for Google QUIC. 126 bool reset_flag; 127 // For Google QUIC, version flag in packets from the server means version 128 // negotiation packet. For IETF QUIC, version flag means long header. 129 bool version_flag; 130 // Indicates whether |possible_stateless_reset_token| contains a valid value 131 // parsed from the packet buffer. IETF QUIC only, always false for GQUIC. 132 bool has_possible_stateless_reset_token; 133 QuicPacketNumberLength packet_number_length; 134 uint8_t type_byte; 135 ParsedQuicVersion version; 136 // nonce contains an optional, 32-byte nonce value. If not included in the 137 // packet, |nonce| will be empty. 138 DiversificationNonce* nonce; 139 QuicPacketNumber packet_number; 140 // Format of this header. 141 PacketHeaderFormat form; 142 // Short packet type is reflected in packet_number_length. 143 QuicLongHeaderType long_packet_type; 144 // Only valid if |has_possible_stateless_reset_token| is true. 145 // Stores last 16 bytes of a this packet, used to check whether this packet is 146 // a stateless reset packet on decryption failure. 147 StatelessResetToken possible_stateless_reset_token; 148 // Length of the retry token length variable length integer field, 149 // carried only by v99 IETF Initial packets. 150 quiche::QuicheVariableLengthIntegerLength retry_token_length_length; 151 // Retry token, carried only by v99 IETF Initial packets. 152 absl::string_view retry_token; 153 // Length of the length variable length integer field, 154 // carried only by v99 IETF Initial, 0-RTT and Handshake packets. 155 quiche::QuicheVariableLengthIntegerLength length_length; 156 // Length of the packet number and payload, carried only by v99 IETF Initial, 157 // 0-RTT and Handshake packets. Also includes the length of the 158 // diversification nonce in server to client 0-RTT packets. 159 QuicByteCount remaining_packet_length; 160 161 bool operator==(const QuicPacketHeader& other) const; 162 bool operator!=(const QuicPacketHeader& other) const; 163 }; 164 165 struct QUIC_EXPORT_PRIVATE QuicPublicResetPacket { 166 QuicPublicResetPacket(); 167 explicit QuicPublicResetPacket(QuicConnectionId connection_id); 168 169 QuicConnectionId connection_id; 170 QuicPublicResetNonceProof nonce_proof; 171 QuicSocketAddress client_address; 172 // An arbitrary string to identify an endpoint. Used by clients to 173 // differentiate traffic from Google servers vs Non-google servers. 174 // Will not be used if empty(). 175 std::string endpoint_id; 176 }; 177 178 struct QUIC_EXPORT_PRIVATE QuicVersionNegotiationPacket { 179 QuicVersionNegotiationPacket(); 180 explicit QuicVersionNegotiationPacket(QuicConnectionId connection_id); 181 QuicVersionNegotiationPacket(const QuicVersionNegotiationPacket& other); 182 ~QuicVersionNegotiationPacket(); 183 184 QuicConnectionId connection_id; 185 ParsedQuicVersionVector versions; 186 }; 187 188 struct QUIC_EXPORT_PRIVATE QuicIetfStatelessResetPacket { 189 QuicIetfStatelessResetPacket(); 190 QuicIetfStatelessResetPacket(const QuicPacketHeader& header, 191 StatelessResetToken token); 192 QuicIetfStatelessResetPacket(const QuicIetfStatelessResetPacket& other); 193 ~QuicIetfStatelessResetPacket(); 194 195 QuicPacketHeader header; 196 StatelessResetToken stateless_reset_token; 197 }; 198 199 class QUIC_EXPORT_PRIVATE QuicData { 200 public: 201 // Creates a QuicData from a buffer and length. Does not own the buffer. 202 QuicData(const char* buffer, size_t length); 203 // Creates a QuicData from a buffer and length, 204 // optionally taking ownership of the buffer. 205 QuicData(const char* buffer, size_t length, bool owns_buffer); 206 // Creates a QuicData from a absl::string_view. Does not own the 207 // buffer. 208 QuicData(absl::string_view data); 209 QuicData(const QuicData&) = delete; 210 QuicData& operator=(const QuicData&) = delete; 211 virtual ~QuicData(); 212 AsStringPiece()213 absl::string_view AsStringPiece() const { 214 return absl::string_view(data(), length()); 215 } 216 data()217 const char* data() const { return buffer_; } length()218 size_t length() const { return length_; } 219 220 private: 221 const char* buffer_; 222 size_t length_; 223 bool owns_buffer_; 224 }; 225 226 class QUIC_EXPORT_PRIVATE QuicPacket : public QuicData { 227 public: 228 QuicPacket( 229 char* buffer, size_t length, bool owns_buffer, 230 uint8_t destination_connection_id_length, 231 uint8_t source_connection_id_length, bool includes_version, 232 bool includes_diversification_nonce, 233 QuicPacketNumberLength packet_number_length, 234 quiche::QuicheVariableLengthIntegerLength retry_token_length_length, 235 QuicByteCount retry_token_length, 236 quiche::QuicheVariableLengthIntegerLength length_length); 237 QuicPacket(QuicTransportVersion version, char* buffer, size_t length, 238 bool owns_buffer, const QuicPacketHeader& header); 239 QuicPacket(const QuicPacket&) = delete; 240 QuicPacket& operator=(const QuicPacket&) = delete; 241 242 absl::string_view AssociatedData(QuicTransportVersion version) const; 243 absl::string_view Plaintext(QuicTransportVersion version) const; 244 mutable_data()245 char* mutable_data() { return buffer_; } 246 247 private: 248 char* buffer_; 249 const uint8_t destination_connection_id_length_; 250 const uint8_t source_connection_id_length_; 251 const bool includes_version_; 252 const bool includes_diversification_nonce_; 253 const QuicPacketNumberLength packet_number_length_; 254 const quiche::QuicheVariableLengthIntegerLength retry_token_length_length_; 255 const QuicByteCount retry_token_length_; 256 const quiche::QuicheVariableLengthIntegerLength length_length_; 257 }; 258 259 class QUIC_EXPORT_PRIVATE QuicEncryptedPacket : public QuicData { 260 public: 261 // Creates a QuicEncryptedPacket from a buffer and length. 262 // Does not own the buffer. 263 QuicEncryptedPacket(const char* buffer, size_t length); 264 // Creates a QuicEncryptedPacket from a buffer and length, 265 // optionally taking ownership of the buffer. 266 QuicEncryptedPacket(const char* buffer, size_t length, bool owns_buffer); 267 // Creates a QuicEncryptedPacket from a absl::string_view. 268 // Does not own the buffer. 269 QuicEncryptedPacket(absl::string_view data); 270 271 QuicEncryptedPacket(const QuicEncryptedPacket&) = delete; 272 QuicEncryptedPacket& operator=(const QuicEncryptedPacket&) = delete; 273 274 // Clones the packet into a new packet which owns the buffer. 275 std::unique_ptr<QuicEncryptedPacket> Clone() const; 276 277 // By default, gtest prints the raw bytes of an object. The bool data 278 // member (in the base class QuicData) causes this object to have padding 279 // bytes, which causes the default gtest object printer to read 280 // uninitialize memory. So we need to teach gtest how to print this object. 281 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<( 282 std::ostream& os, const QuicEncryptedPacket& s); 283 }; 284 285 // A received encrypted QUIC packet, with a recorded time of receipt. 286 class QUIC_EXPORT_PRIVATE QuicReceivedPacket : public QuicEncryptedPacket { 287 public: 288 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time); 289 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time, 290 bool owns_buffer); 291 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time, 292 bool owns_buffer, int ttl, bool ttl_valid); 293 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time, 294 bool owns_buffer, int ttl, bool ttl_valid, 295 char* packet_headers, size_t headers_length, 296 bool owns_header_buffer); 297 QuicReceivedPacket(const char* buffer, size_t length, QuicTime receipt_time, 298 bool owns_buffer, int ttl, bool ttl_valid, 299 char* packet_headers, size_t headers_length, 300 bool owns_header_buffer, QuicEcnCodepoint ecn_codepoint); 301 ~QuicReceivedPacket(); 302 QuicReceivedPacket(const QuicReceivedPacket&) = delete; 303 QuicReceivedPacket& operator=(const QuicReceivedPacket&) = delete; 304 305 // Clones the packet into a new packet which owns the buffer. 306 std::unique_ptr<QuicReceivedPacket> Clone() const; 307 308 // Returns the time at which the packet was received. receipt_time()309 QuicTime receipt_time() const { return receipt_time_; } 310 311 // This is the TTL of the packet, assuming ttl_vaild_ is true. ttl()312 int ttl() const { return ttl_; } 313 314 // Start of packet headers. packet_headers()315 char* packet_headers() const { return packet_headers_; } 316 317 // Length of packet headers. headers_length()318 int headers_length() const { return headers_length_; } 319 320 // By default, gtest prints the raw bytes of an object. The bool data 321 // member (in the base class QuicData) causes this object to have padding 322 // bytes, which causes the default gtest object printer to read 323 // uninitialize memory. So we need to teach gtest how to print this object. 324 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<( 325 std::ostream& os, const QuicReceivedPacket& s); 326 ecn_codepoint()327 QuicEcnCodepoint ecn_codepoint() const { return ecn_codepoint_; } 328 329 private: 330 const QuicTime receipt_time_; 331 int ttl_; 332 // Points to the start of packet headers. 333 char* packet_headers_; 334 // Length of packet headers. 335 int headers_length_; 336 // Whether owns the buffer for packet headers. 337 bool owns_header_buffer_; 338 QuicEcnCodepoint ecn_codepoint_; 339 }; 340 341 // SerializedPacket contains information of a serialized(encrypted) packet. 342 // 343 // WARNING: 344 // 345 // If you add a member field to this class, please make sure it is properly 346 // copied in |CopySerializedPacket|. 347 // 348 struct QUIC_EXPORT_PRIVATE SerializedPacket { 349 SerializedPacket(QuicPacketNumber packet_number, 350 QuicPacketNumberLength packet_number_length, 351 const char* encrypted_buffer, 352 QuicPacketLength encrypted_length, bool has_ack, 353 bool has_stop_waiting); 354 355 // Copy constructor & assignment are deleted. Use |CopySerializedPacket| to 356 // make a copy. 357 SerializedPacket(const SerializedPacket& other) = delete; 358 SerializedPacket& operator=(const SerializedPacket& other) = delete; 359 SerializedPacket(SerializedPacket&& other); 360 ~SerializedPacket(); 361 362 // TODO(wub): replace |encrypted_buffer|+|release_encrypted_buffer| by a 363 // QuicOwnedPacketBuffer. 364 // Not owned if |release_encrypted_buffer| is nullptr. Otherwise it is 365 // released by |release_encrypted_buffer| on destruction. 366 const char* encrypted_buffer; 367 QuicPacketLength encrypted_length; 368 std::function<void(const char*)> release_encrypted_buffer; 369 370 QuicFrames retransmittable_frames; 371 QuicFrames nonretransmittable_frames; 372 IsHandshake has_crypto_handshake; 373 QuicPacketNumber packet_number; 374 QuicPacketNumberLength packet_number_length; 375 EncryptionLevel encryption_level; 376 // TODO(fayang): Remove has_ack and has_stop_waiting. 377 bool has_ack; 378 bool has_stop_waiting; 379 bool has_ack_ecn = false; // ack frame contains ECN counts. 380 TransmissionType transmission_type; 381 // The largest acked of the AckFrame in this packet if has_ack is true, 382 // 0 otherwise. 383 QuicPacketNumber largest_acked; 384 // Indicates whether this packet has a copy of ack frame in 385 // nonretransmittable_frames. 386 bool has_ack_frame_copy; 387 bool has_ack_frequency; 388 bool has_message; 389 SerializedPacketFate fate; 390 QuicSocketAddress peer_address; 391 // Sum of bytes from frames that are not retransmissions. This field is only 392 // populated for packets with "mixed frames": at least one frame of a 393 // retransmission type and at least one frame of NOT_RETRANSMISSION type. 394 absl::optional<QuicByteCount> bytes_not_retransmitted; 395 // Only populated if encryption_level is ENCRYPTION_INITIAL. 396 // TODO(b/265777524): remove this. 397 absl::optional<QuicPacketHeader> initial_header; 398 }; 399 400 // Make a copy of |serialized| (including the underlying frames). |copy_buffer| 401 // indicates whether the encrypted buffer should be copied. 402 QUIC_EXPORT_PRIVATE SerializedPacket* CopySerializedPacket( 403 const SerializedPacket& serialized, 404 quiche::QuicheBufferAllocator* allocator, bool copy_buffer); 405 406 // Allocates a new char[] of size |packet.encrypted_length| and copies in 407 // |packet.encrypted_buffer|. 408 QUIC_EXPORT_PRIVATE char* CopyBuffer(const SerializedPacket& packet); 409 // Allocates a new char[] of size |encrypted_length| and copies in 410 // |encrypted_buffer|. 411 QUIC_EXPORT_PRIVATE char* CopyBuffer(const char* encrypted_buffer, 412 QuicPacketLength encrypted_length); 413 414 // Context for an incoming packet. 415 struct QUIC_EXPORT_PRIVATE QuicPerPacketContext { ~QuicPerPacketContextQuicPerPacketContext416 virtual ~QuicPerPacketContext() {} 417 }; 418 419 // ReceivedPacketInfo comprises information obtained by parsing the unencrypted 420 // bytes of a received packet. 421 struct QUIC_EXPORT_PRIVATE ReceivedPacketInfo { 422 ReceivedPacketInfo(const QuicSocketAddress& self_address, 423 const QuicSocketAddress& peer_address, 424 const QuicReceivedPacket& packet); 425 ReceivedPacketInfo(const ReceivedPacketInfo& other) = default; 426 427 ~ReceivedPacketInfo(); 428 429 std::string ToString() const; 430 431 QUIC_EXPORT_PRIVATE friend std::ostream& operator<<( 432 std::ostream& os, const ReceivedPacketInfo& packet_info); 433 434 const QuicSocketAddress& self_address; 435 const QuicSocketAddress& peer_address; 436 const QuicReceivedPacket& packet; 437 438 PacketHeaderFormat form; 439 // This is only used if the form is IETF_QUIC_LONG_HEADER_PACKET. 440 QuicLongHeaderType long_packet_type; 441 bool version_flag; 442 bool use_length_prefix; 443 QuicVersionLabel version_label; 444 ParsedQuicVersion version; 445 QuicConnectionId destination_connection_id; 446 QuicConnectionId source_connection_id; 447 absl::optional<absl::string_view> retry_token; 448 }; 449 450 } // namespace quic 451 452 #endif // QUICHE_QUIC_CORE_QUIC_PACKETS_H_ 453