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