QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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/third_party/quiche/src/quic/core/quic_framer.h" |
| 6 | |
| 7 | #include <cstddef> |
| 8 | #include <cstdint> |
| 9 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 10 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
| 12 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h" |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/quic/core/crypto/null_decrypter.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/crypto/null_encrypter.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
| 22 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
| 23 | #include "net/third_party/quiche/src/quic/core/quic_constants.h" |
| 24 | #include "net/third_party/quiche/src/quic/core/quic_data_reader.h" |
| 25 | #include "net/third_party/quiche/src/quic/core/quic_data_writer.h" |
ianswett | 97b690b | 2019-05-02 15:12:43 -0700 | [diff] [blame] | 26 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | #include "net/third_party/quiche/src/quic/core/quic_socket_address_coder.h" |
| 28 | #include "net/third_party/quiche/src/quic/core/quic_stream_frame_data_producer.h" |
| 29 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 30 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 31 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 32 | #include "net/third_party/quiche/src/quic/platform/api/quic_aligned.h" |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 33 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 34 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 35 | #include "net/third_party/quiche/src/quic/platform/api/quic_client_stats.h" |
| 36 | #include "net/third_party/quiche/src/quic/platform/api/quic_endian.h" |
| 37 | #include "net/third_party/quiche/src/quic/platform/api/quic_fallthrough.h" |
| 38 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 39 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 40 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 41 | #include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h" |
| 42 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 43 | #include "net/third_party/quiche/src/quic/platform/api/quic_stack_trace.h" |
| 44 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 45 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 46 | |
| 47 | namespace quic { |
| 48 | |
| 49 | namespace { |
| 50 | |
| 51 | #define ENDPOINT \ |
| 52 | (perspective_ == Perspective::IS_SERVER ? "Server: " : "Client: ") |
| 53 | |
| 54 | // How much to shift the timestamp in the IETF Ack frame. |
| 55 | // TODO(fkastenholz) when we get real IETF QUIC, need to get |
| 56 | // the currect shift from the transport parameters. |
| 57 | const int kIetfAckTimestampShift = 3; |
| 58 | |
| 59 | // Number of bits the packet number length bits are shifted from the right |
| 60 | // edge of the header. |
| 61 | const uint8_t kPublicHeaderSequenceNumberShift = 4; |
| 62 | |
| 63 | // There are two interpretations for the Frame Type byte in the QUIC protocol, |
| 64 | // resulting in two Frame Types: Special Frame Types and Regular Frame Types. |
| 65 | // |
| 66 | // Regular Frame Types use the Frame Type byte simply. Currently defined |
| 67 | // Regular Frame Types are: |
| 68 | // Padding : 0b 00000000 (0x00) |
| 69 | // ResetStream : 0b 00000001 (0x01) |
| 70 | // ConnectionClose : 0b 00000010 (0x02) |
| 71 | // GoAway : 0b 00000011 (0x03) |
| 72 | // WindowUpdate : 0b 00000100 (0x04) |
| 73 | // Blocked : 0b 00000101 (0x05) |
| 74 | // |
| 75 | // Special Frame Types encode both a Frame Type and corresponding flags |
| 76 | // all in the Frame Type byte. Currently defined Special Frame Types |
| 77 | // are: |
| 78 | // Stream : 0b 1xxxxxxx |
| 79 | // Ack : 0b 01xxxxxx |
| 80 | // |
| 81 | // Semantics of the flag bits above (the x bits) depends on the frame type. |
| 82 | |
| 83 | // Masks to determine if the frame type is a special use |
| 84 | // and for specific special frame types. |
| 85 | const uint8_t kQuicFrameTypeBrokenMask = 0xE0; // 0b 11100000 |
| 86 | const uint8_t kQuicFrameTypeSpecialMask = 0xC0; // 0b 11000000 |
| 87 | const uint8_t kQuicFrameTypeStreamMask = 0x80; |
| 88 | const uint8_t kQuicFrameTypeAckMask = 0x40; |
| 89 | static_assert(kQuicFrameTypeSpecialMask == |
| 90 | (kQuicFrameTypeStreamMask | kQuicFrameTypeAckMask), |
| 91 | "Invalid kQuicFrameTypeSpecialMask"); |
| 92 | |
| 93 | // The stream type format is 1FDOOOSS, where |
| 94 | // F is the fin bit. |
| 95 | // D is the data length bit (0 or 2 bytes). |
| 96 | // OO/OOO are the size of the offset. |
| 97 | // SS is the size of the stream ID. |
| 98 | // Note that the stream encoding can not be determined by inspection. It can |
| 99 | // be determined only by knowing the QUIC Version. |
| 100 | // Stream frame relative shifts and masks for interpreting the stream flags. |
| 101 | // StreamID may be 1, 2, 3, or 4 bytes. |
| 102 | const uint8_t kQuicStreamIdShift = 2; |
| 103 | const uint8_t kQuicStreamIDLengthMask = 0x03; |
| 104 | |
| 105 | // Offset may be 0, 2, 4, or 8 bytes. |
| 106 | const uint8_t kQuicStreamShift = 3; |
| 107 | const uint8_t kQuicStreamOffsetMask = 0x07; |
| 108 | |
| 109 | // Data length may be 0 or 2 bytes. |
| 110 | const uint8_t kQuicStreamDataLengthShift = 1; |
| 111 | const uint8_t kQuicStreamDataLengthMask = 0x01; |
| 112 | |
| 113 | // Fin bit may be set or not. |
| 114 | const uint8_t kQuicStreamFinShift = 1; |
| 115 | const uint8_t kQuicStreamFinMask = 0x01; |
| 116 | |
| 117 | // The format is 01M0LLOO, where |
| 118 | // M if set, there are multiple ack blocks in the frame. |
| 119 | // LL is the size of the largest ack field. |
| 120 | // OO is the size of the ack blocks offset field. |
| 121 | // packet number size shift used in AckFrames. |
| 122 | const uint8_t kQuicSequenceNumberLengthNumBits = 2; |
| 123 | const uint8_t kActBlockLengthOffset = 0; |
| 124 | const uint8_t kLargestAckedOffset = 2; |
| 125 | |
| 126 | // Acks may have only one ack block. |
| 127 | const uint8_t kQuicHasMultipleAckBlocksOffset = 5; |
| 128 | |
| 129 | // Timestamps are 4 bytes followed by 2 bytes. |
| 130 | const uint8_t kQuicNumTimestampsLength = 1; |
| 131 | const uint8_t kQuicFirstTimestampLength = 4; |
| 132 | const uint8_t kQuicTimestampLength = 2; |
| 133 | // Gaps between packet numbers are 1 byte. |
| 134 | const uint8_t kQuicTimestampPacketNumberGapLength = 1; |
| 135 | |
| 136 | // Maximum length of encoded error strings. |
| 137 | const int kMaxErrorStringLength = 256; |
| 138 | |
| 139 | const uint8_t kConnectionIdLengthAdjustment = 3; |
| 140 | const uint8_t kDestinationConnectionIdLengthMask = 0xF0; |
| 141 | const uint8_t kSourceConnectionIdLengthMask = 0x0F; |
| 142 | |
| 143 | // Returns the absolute value of the difference between |a| and |b|. |
| 144 | uint64_t Delta(uint64_t a, uint64_t b) { |
| 145 | // Since these are unsigned numbers, we can't just return abs(a - b) |
| 146 | if (a < b) { |
| 147 | return b - a; |
| 148 | } |
| 149 | return a - b; |
| 150 | } |
| 151 | |
| 152 | uint64_t ClosestTo(uint64_t target, uint64_t a, uint64_t b) { |
| 153 | return (Delta(target, a) < Delta(target, b)) ? a : b; |
| 154 | } |
| 155 | |
| 156 | uint64_t PacketNumberIntervalLength( |
| 157 | const QuicInterval<QuicPacketNumber>& interval) { |
| 158 | if (interval.Empty()) { |
| 159 | return 0u; |
| 160 | } |
| 161 | return interval.max() - interval.min(); |
| 162 | } |
| 163 | |
| 164 | QuicPacketNumberLength ReadSequenceNumberLength(uint8_t flags) { |
| 165 | switch (flags & PACKET_FLAGS_8BYTE_PACKET) { |
| 166 | case PACKET_FLAGS_8BYTE_PACKET: |
| 167 | return PACKET_6BYTE_PACKET_NUMBER; |
| 168 | case PACKET_FLAGS_4BYTE_PACKET: |
| 169 | return PACKET_4BYTE_PACKET_NUMBER; |
| 170 | case PACKET_FLAGS_2BYTE_PACKET: |
| 171 | return PACKET_2BYTE_PACKET_NUMBER; |
| 172 | case PACKET_FLAGS_1BYTE_PACKET: |
| 173 | return PACKET_1BYTE_PACKET_NUMBER; |
| 174 | default: |
| 175 | QUIC_BUG << "Unreachable case statement."; |
| 176 | return PACKET_6BYTE_PACKET_NUMBER; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | QuicPacketNumberLength ReadAckPacketNumberLength(QuicTransportVersion version, |
| 181 | uint8_t flags) { |
| 182 | switch (flags & PACKET_FLAGS_8BYTE_PACKET) { |
| 183 | case PACKET_FLAGS_8BYTE_PACKET: |
| 184 | return PACKET_6BYTE_PACKET_NUMBER; |
| 185 | case PACKET_FLAGS_4BYTE_PACKET: |
| 186 | return PACKET_4BYTE_PACKET_NUMBER; |
| 187 | case PACKET_FLAGS_2BYTE_PACKET: |
| 188 | return PACKET_2BYTE_PACKET_NUMBER; |
| 189 | case PACKET_FLAGS_1BYTE_PACKET: |
| 190 | return PACKET_1BYTE_PACKET_NUMBER; |
| 191 | default: |
| 192 | QUIC_BUG << "Unreachable case statement."; |
| 193 | return PACKET_6BYTE_PACKET_NUMBER; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | uint8_t PacketNumberLengthToOnWireValue( |
| 198 | QuicTransportVersion version, |
| 199 | QuicPacketNumberLength packet_number_length) { |
| 200 | if (version > QUIC_VERSION_44) { |
| 201 | return packet_number_length - 1; |
| 202 | } |
| 203 | switch (packet_number_length) { |
| 204 | case PACKET_1BYTE_PACKET_NUMBER: |
| 205 | return 0; |
| 206 | case PACKET_2BYTE_PACKET_NUMBER: |
| 207 | return 1; |
| 208 | case PACKET_4BYTE_PACKET_NUMBER: |
| 209 | return 2; |
| 210 | default: |
| 211 | QUIC_BUG << "Invalid packet number length."; |
| 212 | return 0; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | bool GetShortHeaderPacketNumberLength( |
| 217 | QuicTransportVersion version, |
| 218 | uint8_t type, |
| 219 | bool infer_packet_header_type_from_version, |
| 220 | QuicPacketNumberLength* packet_number_length) { |
| 221 | DCHECK(!(type & FLAGS_LONG_HEADER)); |
| 222 | const bool two_bits_packet_number_length = |
| 223 | infer_packet_header_type_from_version ? version > QUIC_VERSION_44 |
| 224 | : (type & FLAGS_FIXED_BIT); |
| 225 | if (two_bits_packet_number_length) { |
| 226 | *packet_number_length = |
| 227 | static_cast<QuicPacketNumberLength>((type & 0x03) + 1); |
| 228 | return true; |
| 229 | } |
| 230 | switch (type & 0x07) { |
| 231 | case 0: |
| 232 | *packet_number_length = PACKET_1BYTE_PACKET_NUMBER; |
| 233 | break; |
| 234 | case 1: |
| 235 | *packet_number_length = PACKET_2BYTE_PACKET_NUMBER; |
| 236 | break; |
| 237 | case 2: |
| 238 | *packet_number_length = PACKET_4BYTE_PACKET_NUMBER; |
| 239 | break; |
| 240 | default: |
| 241 | *packet_number_length = PACKET_6BYTE_PACKET_NUMBER; |
| 242 | return false; |
| 243 | } |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | uint8_t LongHeaderTypeToOnWireValue(QuicTransportVersion version, |
| 248 | QuicLongHeaderType type) { |
| 249 | switch (type) { |
| 250 | case INITIAL: |
| 251 | return version > QUIC_VERSION_44 ? 0 : 0x7F; |
| 252 | case ZERO_RTT_PROTECTED: |
| 253 | return version > QUIC_VERSION_44 ? 1 << 4 : 0x7C; |
| 254 | case HANDSHAKE: |
| 255 | return version > QUIC_VERSION_44 ? 2 << 4 : 0x7D; |
| 256 | case RETRY: |
| 257 | return version > QUIC_VERSION_44 ? 3 << 4 : 0x7E; |
| 258 | case VERSION_NEGOTIATION: |
| 259 | return 0xF0; // Value does not matter |
| 260 | default: |
| 261 | QUIC_BUG << "Invalid long header type: " << type; |
| 262 | return 0xFF; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | bool GetLongHeaderType(QuicTransportVersion version, |
| 267 | uint8_t type, |
| 268 | QuicLongHeaderType* long_header_type) { |
| 269 | DCHECK((type & FLAGS_LONG_HEADER) && version != QUIC_VERSION_UNSUPPORTED); |
| 270 | if (version > QUIC_VERSION_44) { |
| 271 | switch ((type & 0x30) >> 4) { |
| 272 | case 0: |
| 273 | *long_header_type = INITIAL; |
| 274 | break; |
| 275 | case 1: |
| 276 | *long_header_type = ZERO_RTT_PROTECTED; |
| 277 | break; |
| 278 | case 2: |
| 279 | *long_header_type = HANDSHAKE; |
| 280 | break; |
| 281 | case 3: |
| 282 | *long_header_type = RETRY; |
| 283 | break; |
| 284 | default: |
| 285 | QUIC_BUG << "Unreachable statement"; |
dschinazi | 072da7c | 2019-05-07 17:57:42 -0700 | [diff] [blame] | 286 | *long_header_type = INVALID_PACKET_TYPE; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | switch (type & 0x7F) { |
| 293 | case 0x7F: |
| 294 | *long_header_type = INITIAL; |
| 295 | break; |
| 296 | case 0x7C: |
| 297 | *long_header_type = ZERO_RTT_PROTECTED; |
| 298 | break; |
| 299 | case 0x7D: |
| 300 | *long_header_type = HANDSHAKE; |
| 301 | break; |
| 302 | case 0x7E: |
| 303 | *long_header_type = RETRY; |
| 304 | break; |
| 305 | default: |
| 306 | // Invalid packet header type. Whether a packet is version negotiation is |
| 307 | // determined by the version field. |
| 308 | *long_header_type = INVALID_PACKET_TYPE; |
| 309 | return false; |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | QuicPacketNumberLength GetLongHeaderPacketNumberLength( |
| 315 | QuicTransportVersion version, |
| 316 | uint8_t type) { |
| 317 | if (version > QUIC_VERSION_44) { |
| 318 | return static_cast<QuicPacketNumberLength>((type & 0x03) + 1); |
| 319 | } |
| 320 | return PACKET_4BYTE_PACKET_NUMBER; |
| 321 | } |
| 322 | |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 323 | // Used to get packet number space before packet gets decrypted. |
| 324 | PacketNumberSpace GetPacketNumberSpace(const QuicPacketHeader& header) { |
| 325 | switch (header.form) { |
| 326 | case GOOGLE_QUIC_PACKET: |
| 327 | QUIC_BUG << "Try to get packet number space of Google QUIC packet"; |
| 328 | break; |
| 329 | case IETF_QUIC_SHORT_HEADER_PACKET: |
| 330 | return APPLICATION_DATA; |
| 331 | case IETF_QUIC_LONG_HEADER_PACKET: |
| 332 | switch (header.long_packet_type) { |
| 333 | case INITIAL: |
| 334 | return INITIAL_DATA; |
| 335 | case HANDSHAKE: |
| 336 | return HANDSHAKE_DATA; |
| 337 | case ZERO_RTT_PROTECTED: |
| 338 | return APPLICATION_DATA; |
| 339 | case VERSION_NEGOTIATION: |
| 340 | case RETRY: |
| 341 | case INVALID_PACKET_TYPE: |
| 342 | QUIC_BUG << "Try to get packet number space of long header type: " |
| 343 | << QuicUtils::QuicLongHeaderTypetoString( |
| 344 | header.long_packet_type); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return NUM_PACKET_NUMBER_SPACES; |
| 350 | } |
| 351 | |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 352 | EncryptionLevel GetEncryptionLevel(const QuicPacketHeader& header) { |
| 353 | switch (header.form) { |
| 354 | case GOOGLE_QUIC_PACKET: |
| 355 | QUIC_BUG << "Cannot determine EncryptionLevel from Google QUIC header"; |
| 356 | break; |
| 357 | case IETF_QUIC_SHORT_HEADER_PACKET: |
| 358 | return ENCRYPTION_FORWARD_SECURE; |
| 359 | case IETF_QUIC_LONG_HEADER_PACKET: |
| 360 | switch (header.long_packet_type) { |
| 361 | case INITIAL: |
| 362 | return ENCRYPTION_INITIAL; |
| 363 | case HANDSHAKE: |
| 364 | return ENCRYPTION_HANDSHAKE; |
| 365 | case ZERO_RTT_PROTECTED: |
| 366 | return ENCRYPTION_ZERO_RTT; |
| 367 | case VERSION_NEGOTIATION: |
| 368 | case RETRY: |
| 369 | case INVALID_PACKET_TYPE: |
| 370 | QUIC_BUG << "No encryption used with type " |
| 371 | << QuicUtils::QuicLongHeaderTypetoString( |
| 372 | header.long_packet_type); |
| 373 | } |
| 374 | } |
| 375 | return NUM_ENCRYPTION_LEVELS; |
| 376 | } |
| 377 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 378 | QuicStringPiece TruncateErrorString(QuicStringPiece error) { |
| 379 | if (error.length() <= kMaxErrorStringLength) { |
| 380 | return error; |
| 381 | } |
| 382 | return QuicStringPiece(error.data(), kMaxErrorStringLength); |
| 383 | } |
| 384 | |
| 385 | size_t TruncatedErrorStringSize(const QuicStringPiece& error) { |
| 386 | if (error.length() < kMaxErrorStringLength) { |
| 387 | return error.length(); |
| 388 | } |
| 389 | return kMaxErrorStringLength; |
| 390 | } |
| 391 | |
| 392 | uint8_t GetConnectionIdLengthValue(QuicConnectionIdLength length) { |
| 393 | if (length == 0) { |
| 394 | return 0; |
| 395 | } |
| 396 | return static_cast<uint8_t>(length - kConnectionIdLengthAdjustment); |
| 397 | } |
| 398 | |
| 399 | bool IsValidPacketNumberLength(QuicPacketNumberLength packet_number_length) { |
| 400 | size_t length = packet_number_length; |
| 401 | return length == 1 || length == 2 || length == 4 || length == 6 || |
| 402 | length == 8; |
| 403 | } |
| 404 | |
| 405 | bool IsValidFullPacketNumber(uint64_t full_packet_number, |
| 406 | QuicTransportVersion version) { |
QUICHE team | 577718a | 2019-03-20 09:00:59 -0700 | [diff] [blame] | 407 | return full_packet_number > 0 || version == QUIC_VERSION_99; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 408 | } |
| 409 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 410 | bool AppendIetfConnectionIdsNew(bool version_flag, |
| 411 | QuicConnectionId destination_connection_id, |
| 412 | QuicConnectionId source_connection_id, |
| 413 | QuicDataWriter* writer) { |
| 414 | if (!version_flag) { |
| 415 | return writer->WriteConnectionId(destination_connection_id); |
| 416 | } |
| 417 | |
| 418 | // Compute connection ID length byte. |
| 419 | uint8_t dcil = GetConnectionIdLengthValue( |
| 420 | static_cast<QuicConnectionIdLength>(destination_connection_id.length())); |
| 421 | uint8_t scil = GetConnectionIdLengthValue( |
| 422 | static_cast<QuicConnectionIdLength>(source_connection_id.length())); |
| 423 | uint8_t connection_id_length = dcil << 4 | scil; |
| 424 | |
| 425 | return writer->WriteUInt8(connection_id_length) && |
| 426 | writer->WriteConnectionId(destination_connection_id) && |
| 427 | writer->WriteConnectionId(source_connection_id); |
| 428 | } |
| 429 | |
| 430 | enum class DroppedPacketReason { |
| 431 | // General errors |
| 432 | INVALID_PUBLIC_HEADER, |
| 433 | VERSION_MISMATCH, |
| 434 | // Version negotiation packet errors |
| 435 | INVALID_VERSION_NEGOTIATION_PACKET, |
| 436 | // Public reset packet errors, pre-v44 |
| 437 | INVALID_PUBLIC_RESET_PACKET, |
| 438 | // Data packet errors |
| 439 | INVALID_PACKET_NUMBER, |
| 440 | INVALID_DIVERSIFICATION_NONCE, |
| 441 | DECRYPTION_FAILURE, |
| 442 | NUM_REASONS, |
| 443 | }; |
| 444 | |
| 445 | void RecordDroppedPacketReason(DroppedPacketReason reason) { |
| 446 | QUIC_CLIENT_HISTOGRAM_ENUM("QuicDroppedPacketReason", reason, |
| 447 | DroppedPacketReason::NUM_REASONS, |
| 448 | "The reason a packet was not processed. Recorded " |
| 449 | "each time such a packet is dropped"); |
| 450 | } |
| 451 | |
| 452 | } // namespace |
| 453 | |
| 454 | QuicFramer::QuicFramer(const ParsedQuicVersionVector& supported_versions, |
| 455 | QuicTime creation_time, |
| 456 | Perspective perspective, |
| 457 | uint8_t expected_connection_id_length) |
| 458 | : visitor_(nullptr), |
| 459 | error_(QUIC_NO_ERROR), |
| 460 | last_serialized_connection_id_(EmptyQuicConnectionId()), |
| 461 | last_version_label_(0), |
| 462 | version_(PROTOCOL_UNSUPPORTED, QUIC_VERSION_UNSUPPORTED), |
| 463 | supported_versions_(supported_versions), |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 464 | decrypter_level_(ENCRYPTION_INITIAL), |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 465 | alternative_decrypter_level_(NUM_ENCRYPTION_LEVELS), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | alternative_decrypter_latch_(false), |
| 467 | perspective_(perspective), |
| 468 | validate_flags_(true), |
| 469 | process_timestamps_(false), |
| 470 | creation_time_(creation_time), |
| 471 | last_timestamp_(QuicTime::Delta::Zero()), |
| 472 | first_sending_packet_number_(FirstSendingPacketNumber()), |
| 473 | data_producer_(nullptr), |
| 474 | infer_packet_header_type_from_version_(perspective == |
| 475 | Perspective::IS_CLIENT), |
QUICHE team | 4d9d629 | 2019-03-11 14:25:33 -0700 | [diff] [blame] | 476 | expected_connection_id_length_(expected_connection_id_length), |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 477 | should_update_expected_connection_id_length_(false), |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 478 | supports_multiple_packet_number_spaces_(false), |
| 479 | last_written_packet_number_length_(0) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 480 | DCHECK(!supported_versions.empty()); |
| 481 | version_ = supported_versions_[0]; |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 482 | decrypter_[ENCRYPTION_INITIAL] = QuicMakeUnique<NullDecrypter>(perspective); |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 483 | encrypter_[ENCRYPTION_INITIAL] = QuicMakeUnique<NullEncrypter>(perspective); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | QuicFramer::~QuicFramer() {} |
| 487 | |
| 488 | // static |
| 489 | size_t QuicFramer::GetMinStreamFrameSize(QuicTransportVersion version, |
| 490 | QuicStreamId stream_id, |
| 491 | QuicStreamOffset offset, |
| 492 | bool last_frame_in_packet, |
| 493 | QuicPacketLength data_length) { |
| 494 | if (version == QUIC_VERSION_99) { |
| 495 | return kQuicFrameTypeSize + QuicDataWriter::GetVarInt62Len(stream_id) + |
| 496 | (last_frame_in_packet |
| 497 | ? 0 |
| 498 | : QuicDataWriter::GetVarInt62Len(data_length)) + |
| 499 | (offset != 0 ? QuicDataWriter::GetVarInt62Len(offset) : 0); |
| 500 | } |
| 501 | return kQuicFrameTypeSize + GetStreamIdSize(stream_id) + |
| 502 | GetStreamOffsetSize(version, offset) + |
| 503 | (last_frame_in_packet ? 0 : kQuicStreamPayloadLengthSize); |
| 504 | } |
| 505 | |
| 506 | // static |
| 507 | size_t QuicFramer::GetMinCryptoFrameSize(QuicStreamOffset offset, |
| 508 | QuicPacketLength data_length) { |
| 509 | return kQuicFrameTypeSize + QuicDataWriter::GetVarInt62Len(offset) + |
| 510 | QuicDataWriter::GetVarInt62Len(data_length); |
| 511 | } |
| 512 | |
| 513 | // static |
| 514 | size_t QuicFramer::GetMessageFrameSize(QuicTransportVersion version, |
| 515 | bool last_frame_in_packet, |
| 516 | QuicByteCount length) { |
| 517 | QUIC_BUG_IF(version <= QUIC_VERSION_44) |
| 518 | << "Try to serialize MESSAGE frame in " << version; |
| 519 | return kQuicFrameTypeSize + |
| 520 | (last_frame_in_packet ? 0 : QuicDataWriter::GetVarInt62Len(length)) + |
| 521 | length; |
| 522 | } |
| 523 | |
| 524 | // static |
| 525 | size_t QuicFramer::GetMinAckFrameSize( |
| 526 | QuicTransportVersion version, |
| 527 | QuicPacketNumberLength largest_observed_length) { |
| 528 | if (version == QUIC_VERSION_99) { |
| 529 | // The minimal ack frame consists of the following four fields: Largest |
| 530 | // Acknowledged, ACK Delay, ACK Block Count, and First ACK Block. Minimum |
| 531 | // size of each is 1 byte. |
| 532 | return kQuicFrameTypeSize + 4; |
| 533 | } |
| 534 | size_t min_size = kQuicFrameTypeSize + largest_observed_length + |
| 535 | kQuicDeltaTimeLargestObservedSize; |
| 536 | return min_size + kQuicNumTimestampsSize; |
| 537 | } |
| 538 | |
| 539 | // static |
| 540 | size_t QuicFramer::GetStopWaitingFrameSize( |
| 541 | QuicTransportVersion version, |
| 542 | QuicPacketNumberLength packet_number_length) { |
| 543 | size_t min_size = kQuicFrameTypeSize + packet_number_length; |
| 544 | return min_size; |
| 545 | } |
| 546 | |
| 547 | // static |
| 548 | size_t QuicFramer::GetRstStreamFrameSize(QuicTransportVersion version, |
| 549 | const QuicRstStreamFrame& frame) { |
| 550 | if (version == QUIC_VERSION_99) { |
| 551 | return QuicDataWriter::GetVarInt62Len(frame.stream_id) + |
| 552 | QuicDataWriter::GetVarInt62Len(frame.byte_offset) + |
| 553 | kQuicFrameTypeSize + kQuicIetfQuicErrorCodeSize; |
| 554 | } |
| 555 | return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize + |
| 556 | kQuicErrorCodeSize; |
| 557 | } |
| 558 | |
| 559 | // static |
fkastenholz | a037b8b | 2019-05-07 06:00:05 -0700 | [diff] [blame] | 560 | size_t QuicFramer::GetConnectionCloseFrameSize( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 561 | QuicTransportVersion version, |
| 562 | const QuicConnectionCloseFrame& frame) { |
fkastenholz | a037b8b | 2019-05-07 06:00:05 -0700 | [diff] [blame] | 563 | if (version != QUIC_VERSION_99) { |
| 564 | // Not version 99/IETF QUIC, return Google QUIC CONNECTION CLOSE frame size. |
| 565 | return kQuicFrameTypeSize + kQuicErrorCodeSize + |
| 566 | kQuicErrorDetailsLengthSize + |
| 567 | TruncatedErrorStringSize(frame.error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 568 | } |
fkastenholz | a037b8b | 2019-05-07 06:00:05 -0700 | [diff] [blame] | 569 | // TODO(fkastenholz): For complete support of IETF QUIC CONNECTION_CLOSE, |
| 570 | // check if the frame is a Transport close and if the frame's |
| 571 | // extracted_error_code is not QUIC_IETF_GQUIC_ERROR_MISSING. If so, |
| 572 | // extend the error string to include " QuicErrorCode: #" |
| 573 | const size_t truncated_error_string_size = |
| 574 | TruncatedErrorStringSize(frame.error_details); |
| 575 | const size_t frame_size = |
| 576 | truncated_error_string_size + |
| 577 | QuicDataWriter::GetVarInt62Len(truncated_error_string_size) + |
| 578 | kQuicFrameTypeSize + kQuicIetfQuicErrorCodeSize; |
| 579 | if (frame.close_type == IETF_QUIC_APPLICATION_CONNECTION_CLOSE) { |
| 580 | return frame_size; |
| 581 | } |
| 582 | // frame includes the transport_close_frame_type, so include its length. |
| 583 | return frame_size + |
| 584 | QuicDataWriter::GetVarInt62Len(frame.transport_close_frame_type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // static |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 588 | size_t QuicFramer::GetMinGoAwayFrameSize() { |
| 589 | return kQuicFrameTypeSize + kQuicErrorCodeSize + kQuicErrorDetailsLengthSize + |
| 590 | kQuicMaxStreamIdSize; |
| 591 | } |
| 592 | |
| 593 | // static |
| 594 | size_t QuicFramer::GetWindowUpdateFrameSize( |
| 595 | QuicTransportVersion version, |
| 596 | const QuicWindowUpdateFrame& frame) { |
| 597 | if (version != QUIC_VERSION_99) { |
| 598 | return kQuicFrameTypeSize + kQuicMaxStreamIdSize + kQuicMaxStreamOffsetSize; |
| 599 | } |
| 600 | if (frame.stream_id == QuicUtils::GetInvalidStreamId(version)) { |
| 601 | // Frame would be a MAX DATA frame, which has only a Maximum Data field. |
| 602 | return kQuicFrameTypeSize + |
| 603 | QuicDataWriter::GetVarInt62Len(frame.byte_offset); |
| 604 | } |
| 605 | // Frame would be MAX STREAM DATA, has Maximum Stream Data and Stream ID |
| 606 | // fields. |
| 607 | return kQuicFrameTypeSize + |
| 608 | QuicDataWriter::GetVarInt62Len(frame.byte_offset) + |
| 609 | QuicDataWriter::GetVarInt62Len(frame.stream_id); |
| 610 | } |
| 611 | |
| 612 | // static |
| 613 | size_t QuicFramer::GetMaxStreamsFrameSize(QuicTransportVersion version, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 614 | const QuicMaxStreamsFrame& frame) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 615 | if (version != QUIC_VERSION_99) { |
| 616 | QUIC_BUG << "In version " << version |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 617 | << " - not 99 - and tried to serialize MaxStreams Frame."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 618 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 619 | return kQuicFrameTypeSize + |
| 620 | QuicDataWriter::GetVarInt62Len(frame.stream_count); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // static |
| 624 | size_t QuicFramer::GetStreamsBlockedFrameSize( |
| 625 | QuicTransportVersion version, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 626 | const QuicStreamsBlockedFrame& frame) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 627 | if (version != QUIC_VERSION_99) { |
| 628 | QUIC_BUG << "In version " << version |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 629 | << " - not 99 - and tried to serialize StreamsBlocked Frame."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 630 | } |
| 631 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 632 | return kQuicFrameTypeSize + |
| 633 | QuicDataWriter::GetVarInt62Len(frame.stream_count); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // static |
| 637 | size_t QuicFramer::GetBlockedFrameSize(QuicTransportVersion version, |
| 638 | const QuicBlockedFrame& frame) { |
| 639 | if (version != QUIC_VERSION_99) { |
| 640 | return kQuicFrameTypeSize + kQuicMaxStreamIdSize; |
| 641 | } |
| 642 | if (frame.stream_id == QuicUtils::GetInvalidStreamId(version)) { |
| 643 | // return size of IETF QUIC Blocked frame |
| 644 | return kQuicFrameTypeSize + QuicDataWriter::GetVarInt62Len(frame.offset); |
| 645 | } |
| 646 | // return size of IETF QUIC Stream Blocked frame. |
| 647 | return kQuicFrameTypeSize + QuicDataWriter::GetVarInt62Len(frame.offset) + |
| 648 | QuicDataWriter::GetVarInt62Len(frame.stream_id); |
| 649 | } |
| 650 | |
| 651 | // static |
| 652 | size_t QuicFramer::GetStopSendingFrameSize(const QuicStopSendingFrame& frame) { |
| 653 | return kQuicFrameTypeSize + QuicDataWriter::GetVarInt62Len(frame.stream_id) + |
| 654 | sizeof(QuicApplicationErrorCode); |
| 655 | } |
| 656 | |
| 657 | // static |
| 658 | size_t QuicFramer::GetPathChallengeFrameSize( |
| 659 | const QuicPathChallengeFrame& frame) { |
| 660 | return kQuicFrameTypeSize + sizeof(frame.data_buffer); |
| 661 | } |
| 662 | |
| 663 | // static |
| 664 | size_t QuicFramer::GetPathResponseFrameSize( |
| 665 | const QuicPathResponseFrame& frame) { |
| 666 | return kQuicFrameTypeSize + sizeof(frame.data_buffer); |
| 667 | } |
| 668 | |
| 669 | // static |
| 670 | size_t QuicFramer::GetRetransmittableControlFrameSize( |
| 671 | QuicTransportVersion version, |
| 672 | const QuicFrame& frame) { |
| 673 | switch (frame.type) { |
| 674 | case PING_FRAME: |
| 675 | // Ping has no payload. |
| 676 | return kQuicFrameTypeSize; |
| 677 | case RST_STREAM_FRAME: |
| 678 | return GetRstStreamFrameSize(version, *frame.rst_stream_frame); |
| 679 | case CONNECTION_CLOSE_FRAME: |
fkastenholz | a037b8b | 2019-05-07 06:00:05 -0700 | [diff] [blame] | 680 | return GetConnectionCloseFrameSize(version, |
| 681 | *frame.connection_close_frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 682 | case GOAWAY_FRAME: |
| 683 | return GetMinGoAwayFrameSize() + |
| 684 | TruncatedErrorStringSize(frame.goaway_frame->reason_phrase); |
| 685 | case WINDOW_UPDATE_FRAME: |
| 686 | // For version 99, this could be either a MAX DATA or MAX STREAM DATA. |
| 687 | // GetWindowUpdateFrameSize figures this out and returns the correct |
| 688 | // length. |
| 689 | return GetWindowUpdateFrameSize(version, *frame.window_update_frame); |
| 690 | case BLOCKED_FRAME: |
| 691 | return GetBlockedFrameSize(version, *frame.blocked_frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 692 | case NEW_CONNECTION_ID_FRAME: |
| 693 | return GetNewConnectionIdFrameSize(*frame.new_connection_id_frame); |
| 694 | case RETIRE_CONNECTION_ID_FRAME: |
| 695 | return GetRetireConnectionIdFrameSize(*frame.retire_connection_id_frame); |
| 696 | case NEW_TOKEN_FRAME: |
| 697 | return GetNewTokenFrameSize(*frame.new_token_frame); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 698 | case MAX_STREAMS_FRAME: |
| 699 | return GetMaxStreamsFrameSize(version, frame.max_streams_frame); |
| 700 | case STREAMS_BLOCKED_FRAME: |
| 701 | return GetStreamsBlockedFrameSize(version, frame.streams_blocked_frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 702 | case PATH_RESPONSE_FRAME: |
| 703 | return GetPathResponseFrameSize(*frame.path_response_frame); |
| 704 | case PATH_CHALLENGE_FRAME: |
| 705 | return GetPathChallengeFrameSize(*frame.path_challenge_frame); |
| 706 | case STOP_SENDING_FRAME: |
| 707 | return GetStopSendingFrameSize(*frame.stop_sending_frame); |
| 708 | |
| 709 | case STREAM_FRAME: |
| 710 | case ACK_FRAME: |
| 711 | case STOP_WAITING_FRAME: |
| 712 | case MTU_DISCOVERY_FRAME: |
| 713 | case PADDING_FRAME: |
| 714 | case MESSAGE_FRAME: |
| 715 | case CRYPTO_FRAME: |
| 716 | case NUM_FRAME_TYPES: |
| 717 | DCHECK(false); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | // Not reachable, but some Chrome compilers can't figure that out. *sigh* |
| 722 | DCHECK(false); |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | // static |
| 727 | size_t QuicFramer::GetStreamIdSize(QuicStreamId stream_id) { |
| 728 | // Sizes are 1 through 4 bytes. |
| 729 | for (int i = 1; i <= 4; ++i) { |
| 730 | stream_id >>= 8; |
| 731 | if (stream_id == 0) { |
| 732 | return i; |
| 733 | } |
| 734 | } |
| 735 | QUIC_BUG << "Failed to determine StreamIDSize."; |
| 736 | return 4; |
| 737 | } |
| 738 | |
| 739 | // static |
| 740 | size_t QuicFramer::GetStreamOffsetSize(QuicTransportVersion version, |
| 741 | QuicStreamOffset offset) { |
| 742 | // 0 is a special case. |
| 743 | if (offset == 0) { |
| 744 | return 0; |
| 745 | } |
| 746 | // 2 through 8 are the remaining sizes. |
| 747 | offset >>= 8; |
| 748 | for (int i = 2; i <= 8; ++i) { |
| 749 | offset >>= 8; |
| 750 | if (offset == 0) { |
| 751 | return i; |
| 752 | } |
| 753 | } |
| 754 | QUIC_BUG << "Failed to determine StreamOffsetSize."; |
| 755 | return 8; |
| 756 | } |
| 757 | |
| 758 | // static |
| 759 | size_t QuicFramer::GetNewConnectionIdFrameSize( |
| 760 | const QuicNewConnectionIdFrame& frame) { |
| 761 | return kQuicFrameTypeSize + |
| 762 | QuicDataWriter::GetVarInt62Len(frame.sequence_number) + |
| 763 | kConnectionIdLengthSize + frame.connection_id.length() + |
| 764 | sizeof(frame.stateless_reset_token); |
| 765 | } |
| 766 | |
| 767 | // static |
| 768 | size_t QuicFramer::GetRetireConnectionIdFrameSize( |
| 769 | const QuicRetireConnectionIdFrame& frame) { |
| 770 | return kQuicFrameTypeSize + |
| 771 | QuicDataWriter::GetVarInt62Len(frame.sequence_number); |
| 772 | } |
| 773 | |
| 774 | // static |
| 775 | size_t QuicFramer::GetNewTokenFrameSize(const QuicNewTokenFrame& frame) { |
| 776 | return kQuicFrameTypeSize + |
| 777 | QuicDataWriter::GetVarInt62Len(frame.token.length()) + |
| 778 | frame.token.length(); |
| 779 | } |
| 780 | |
| 781 | // TODO(nharper): Change this method to take a ParsedQuicVersion. |
| 782 | bool QuicFramer::IsSupportedTransportVersion( |
| 783 | const QuicTransportVersion version) const { |
| 784 | for (ParsedQuicVersion supported_version : supported_versions_) { |
| 785 | if (version == supported_version.transport_version) { |
| 786 | return true; |
| 787 | } |
| 788 | } |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | bool QuicFramer::IsSupportedVersion(const ParsedQuicVersion version) const { |
| 793 | for (const ParsedQuicVersion& supported_version : supported_versions_) { |
| 794 | if (version == supported_version) { |
| 795 | return true; |
| 796 | } |
| 797 | } |
| 798 | return false; |
| 799 | } |
| 800 | |
| 801 | size_t QuicFramer::GetSerializedFrameLength( |
| 802 | const QuicFrame& frame, |
| 803 | size_t free_bytes, |
| 804 | bool first_frame, |
| 805 | bool last_frame, |
| 806 | QuicPacketNumberLength packet_number_length) { |
| 807 | // Prevent a rare crash reported in b/19458523. |
| 808 | if (frame.type == ACK_FRAME && frame.ack_frame == nullptr) { |
| 809 | QUIC_BUG << "Cannot compute the length of a null ack frame. free_bytes:" |
| 810 | << free_bytes << " first_frame:" << first_frame |
| 811 | << " last_frame:" << last_frame |
| 812 | << " seq num length:" << packet_number_length; |
| 813 | set_error(QUIC_INTERNAL_ERROR); |
| 814 | visitor_->OnError(this); |
| 815 | return 0; |
| 816 | } |
| 817 | if (frame.type == PADDING_FRAME) { |
| 818 | if (frame.padding_frame.num_padding_bytes == -1) { |
| 819 | // Full padding to the end of the packet. |
| 820 | return free_bytes; |
| 821 | } else { |
| 822 | // Lite padding. |
| 823 | return free_bytes < |
| 824 | static_cast<size_t>(frame.padding_frame.num_padding_bytes) |
| 825 | ? free_bytes |
| 826 | : frame.padding_frame.num_padding_bytes; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | size_t frame_len = |
| 831 | ComputeFrameLength(frame, last_frame, packet_number_length); |
| 832 | if (frame_len <= free_bytes) { |
| 833 | // Frame fits within packet. Note that acks may be truncated. |
| 834 | return frame_len; |
| 835 | } |
| 836 | // Only truncate the first frame in a packet, so if subsequent ones go |
| 837 | // over, stop including more frames. |
| 838 | if (!first_frame) { |
| 839 | return 0; |
| 840 | } |
| 841 | bool can_truncate = |
| 842 | frame.type == ACK_FRAME && |
| 843 | free_bytes >= GetMinAckFrameSize(version_.transport_version, |
| 844 | PACKET_6BYTE_PACKET_NUMBER); |
| 845 | if (can_truncate) { |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 846 | // Truncate the frame so the packet will not exceed kMaxOutgoingPacketSize. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 847 | // Note that we may not use every byte of the writer in this case. |
| 848 | QUIC_DLOG(INFO) << ENDPOINT |
| 849 | << "Truncating large frame, free bytes: " << free_bytes; |
| 850 | return free_bytes; |
| 851 | } |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | QuicFramer::AckFrameInfo::AckFrameInfo() |
| 856 | : max_block_length(0), first_block_length(0), num_ack_blocks(0) {} |
| 857 | |
| 858 | QuicFramer::AckFrameInfo::AckFrameInfo(const AckFrameInfo& other) = default; |
| 859 | |
| 860 | QuicFramer::AckFrameInfo::~AckFrameInfo() {} |
| 861 | |
| 862 | bool QuicFramer::WriteIetfLongHeaderLength(const QuicPacketHeader& header, |
| 863 | QuicDataWriter* writer, |
| 864 | size_t length_field_offset, |
| 865 | EncryptionLevel level) { |
| 866 | if (!QuicVersionHasLongHeaderLengths(transport_version()) || |
| 867 | !header.version_flag || length_field_offset == 0) { |
| 868 | return true; |
| 869 | } |
| 870 | if (writer->length() < length_field_offset || |
| 871 | writer->length() - length_field_offset < |
| 872 | kQuicDefaultLongHeaderLengthLength) { |
| 873 | set_detailed_error("Invalid length_field_offset."); |
| 874 | QUIC_BUG << "Invalid length_field_offset."; |
| 875 | return false; |
| 876 | } |
| 877 | size_t length_to_write = writer->length() - length_field_offset - |
| 878 | kQuicDefaultLongHeaderLengthLength; |
| 879 | // Add length of auth tag. |
| 880 | length_to_write = GetCiphertextSize(level, length_to_write); |
| 881 | |
| 882 | QuicDataWriter length_writer(writer->length() - length_field_offset, |
| 883 | writer->data() + length_field_offset); |
| 884 | if (!length_writer.WriteVarInt62(length_to_write, |
| 885 | kQuicDefaultLongHeaderLengthLength)) { |
| 886 | set_detailed_error("Failed to overwrite long header length."); |
| 887 | QUIC_BUG << "Failed to overwrite long header length."; |
| 888 | return false; |
| 889 | } |
| 890 | return true; |
| 891 | } |
| 892 | |
| 893 | size_t QuicFramer::BuildDataPacket(const QuicPacketHeader& header, |
| 894 | const QuicFrames& frames, |
| 895 | char* buffer, |
| 896 | size_t packet_length, |
| 897 | EncryptionLevel level) { |
| 898 | QuicDataWriter writer(packet_length, buffer); |
| 899 | size_t length_field_offset = 0; |
| 900 | if (!AppendPacketHeader(header, &writer, &length_field_offset)) { |
| 901 | QUIC_BUG << "AppendPacketHeader failed"; |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | if (transport_version() == QUIC_VERSION_99) { |
| 906 | if (AppendIetfFrames(frames, &writer) == 0) { |
| 907 | return 0; |
| 908 | } |
| 909 | if (!WriteIetfLongHeaderLength(header, &writer, length_field_offset, |
| 910 | level)) { |
| 911 | return 0; |
| 912 | } |
| 913 | return writer.length(); |
| 914 | } |
| 915 | // TODO(dschinazi) if we enable long header lengths before v99, we need to |
| 916 | // add support for fixing up lengths in QuicFramer::BuildDataPacket. |
| 917 | DCHECK(!QuicVersionHasLongHeaderLengths(transport_version())); |
| 918 | |
| 919 | size_t i = 0; |
| 920 | for (const QuicFrame& frame : frames) { |
| 921 | // Determine if we should write stream frame length in header. |
| 922 | const bool last_frame_in_packet = i == frames.size() - 1; |
| 923 | if (!AppendTypeByte(frame, last_frame_in_packet, &writer)) { |
| 924 | QUIC_BUG << "AppendTypeByte failed"; |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | switch (frame.type) { |
| 929 | case PADDING_FRAME: |
| 930 | if (!AppendPaddingFrame(frame.padding_frame, &writer)) { |
| 931 | QUIC_BUG << "AppendPaddingFrame of " |
| 932 | << frame.padding_frame.num_padding_bytes << " failed"; |
| 933 | return 0; |
| 934 | } |
| 935 | break; |
| 936 | case STREAM_FRAME: |
| 937 | if (!AppendStreamFrame(frame.stream_frame, last_frame_in_packet, |
| 938 | &writer)) { |
| 939 | QUIC_BUG << "AppendStreamFrame failed"; |
| 940 | return 0; |
| 941 | } |
| 942 | break; |
| 943 | case ACK_FRAME: |
| 944 | if (!AppendAckFrameAndTypeByte(*frame.ack_frame, &writer)) { |
| 945 | QUIC_BUG << "AppendAckFrameAndTypeByte failed: " << detailed_error_; |
| 946 | return 0; |
| 947 | } |
| 948 | break; |
| 949 | case STOP_WAITING_FRAME: |
| 950 | if (!AppendStopWaitingFrame(header, frame.stop_waiting_frame, |
| 951 | &writer)) { |
| 952 | QUIC_BUG << "AppendStopWaitingFrame failed"; |
| 953 | return 0; |
| 954 | } |
| 955 | break; |
| 956 | case MTU_DISCOVERY_FRAME: |
| 957 | // MTU discovery frames are serialized as ping frames. |
| 958 | QUIC_FALLTHROUGH_INTENDED; |
| 959 | case PING_FRAME: |
| 960 | // Ping has no payload. |
| 961 | break; |
| 962 | case RST_STREAM_FRAME: |
| 963 | if (!AppendRstStreamFrame(*frame.rst_stream_frame, &writer)) { |
| 964 | QUIC_BUG << "AppendRstStreamFrame failed"; |
| 965 | return 0; |
| 966 | } |
| 967 | break; |
| 968 | case CONNECTION_CLOSE_FRAME: |
| 969 | if (!AppendConnectionCloseFrame(*frame.connection_close_frame, |
| 970 | &writer)) { |
| 971 | QUIC_BUG << "AppendConnectionCloseFrame failed"; |
| 972 | return 0; |
| 973 | } |
| 974 | break; |
| 975 | case GOAWAY_FRAME: |
| 976 | if (!AppendGoAwayFrame(*frame.goaway_frame, &writer)) { |
| 977 | QUIC_BUG << "AppendGoAwayFrame failed"; |
| 978 | return 0; |
| 979 | } |
| 980 | break; |
| 981 | case WINDOW_UPDATE_FRAME: |
| 982 | if (!AppendWindowUpdateFrame(*frame.window_update_frame, &writer)) { |
| 983 | QUIC_BUG << "AppendWindowUpdateFrame failed"; |
| 984 | return 0; |
| 985 | } |
| 986 | break; |
| 987 | case BLOCKED_FRAME: |
| 988 | if (!AppendBlockedFrame(*frame.blocked_frame, &writer)) { |
| 989 | QUIC_BUG << "AppendBlockedFrame failed"; |
| 990 | return 0; |
| 991 | } |
| 992 | break; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 993 | case NEW_CONNECTION_ID_FRAME: |
| 994 | set_detailed_error( |
| 995 | "Attempt to append NEW_CONNECTION_ID frame and not in version 99."); |
| 996 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 997 | case RETIRE_CONNECTION_ID_FRAME: |
| 998 | set_detailed_error( |
| 999 | "Attempt to append RETIRE_CONNECTION_ID frame and not in version " |
| 1000 | "99."); |
| 1001 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1002 | case NEW_TOKEN_FRAME: |
| 1003 | set_detailed_error( |
| 1004 | "Attempt to append NEW_TOKEN_ID frame and not in version 99."); |
| 1005 | return RaiseError(QUIC_INTERNAL_ERROR); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1006 | case MAX_STREAMS_FRAME: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1007 | set_detailed_error( |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1008 | "Attempt to append MAX_STREAMS frame and not in version 99."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1009 | return RaiseError(QUIC_INTERNAL_ERROR); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1010 | case STREAMS_BLOCKED_FRAME: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1011 | set_detailed_error( |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1012 | "Attempt to append STREAMS_BLOCKED frame and not in version 99."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1013 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1014 | case PATH_RESPONSE_FRAME: |
| 1015 | set_detailed_error( |
| 1016 | "Attempt to append PATH_RESPONSE frame and not in version 99."); |
| 1017 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1018 | case PATH_CHALLENGE_FRAME: |
| 1019 | set_detailed_error( |
| 1020 | "Attempt to append PATH_CHALLENGE frame and not in version 99."); |
| 1021 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1022 | case STOP_SENDING_FRAME: |
| 1023 | set_detailed_error( |
| 1024 | "Attempt to append STOP_SENDING frame and not in version 99."); |
| 1025 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1026 | case MESSAGE_FRAME: |
| 1027 | if (!AppendMessageFrameAndTypeByte(*frame.message_frame, |
| 1028 | last_frame_in_packet, &writer)) { |
| 1029 | QUIC_BUG << "AppendMessageFrame failed"; |
| 1030 | return 0; |
| 1031 | } |
| 1032 | break; |
| 1033 | case CRYPTO_FRAME: |
QUICHE team | ea74008 | 2019-03-11 17:58:43 -0700 | [diff] [blame] | 1034 | if (!QuicVersionUsesCryptoFrames(version_.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1035 | set_detailed_error( |
| 1036 | "Attempt to append CRYPTO frame in version prior to 47."); |
| 1037 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1038 | } |
| 1039 | if (!AppendCryptoFrame(*frame.crypto_frame, &writer)) { |
| 1040 | QUIC_BUG << "AppendCryptoFrame failed"; |
| 1041 | return 0; |
| 1042 | } |
| 1043 | break; |
| 1044 | default: |
| 1045 | RaiseError(QUIC_INVALID_FRAME_DATA); |
| 1046 | QUIC_BUG << "QUIC_INVALID_FRAME_DATA"; |
| 1047 | return 0; |
| 1048 | } |
| 1049 | ++i; |
| 1050 | } |
| 1051 | |
| 1052 | return writer.length(); |
| 1053 | } |
| 1054 | |
| 1055 | size_t QuicFramer::AppendIetfFrames(const QuicFrames& frames, |
| 1056 | QuicDataWriter* writer) { |
| 1057 | size_t i = 0; |
| 1058 | for (const QuicFrame& frame : frames) { |
| 1059 | // Determine if we should write stream frame length in header. |
| 1060 | const bool last_frame_in_packet = i == frames.size() - 1; |
| 1061 | if (!AppendIetfTypeByte(frame, last_frame_in_packet, writer)) { |
| 1062 | QUIC_BUG << "AppendIetfTypeByte failed: " << detailed_error(); |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
| 1066 | switch (frame.type) { |
| 1067 | case PADDING_FRAME: |
| 1068 | if (!AppendPaddingFrame(frame.padding_frame, writer)) { |
| 1069 | QUIC_BUG << "AppendPaddingFrame of " |
| 1070 | << frame.padding_frame.num_padding_bytes |
| 1071 | << " failed: " << detailed_error(); |
| 1072 | return 0; |
| 1073 | } |
| 1074 | break; |
| 1075 | case STREAM_FRAME: |
| 1076 | if (!AppendStreamFrame(frame.stream_frame, last_frame_in_packet, |
| 1077 | writer)) { |
| 1078 | QUIC_BUG << "AppendStreamFrame failed: " << detailed_error(); |
| 1079 | return 0; |
| 1080 | } |
| 1081 | break; |
| 1082 | case ACK_FRAME: |
| 1083 | if (!AppendIetfAckFrameAndTypeByte(*frame.ack_frame, writer)) { |
QUICHE team | 4fe0b94 | 2019-03-08 09:25:06 -0500 | [diff] [blame] | 1084 | QUIC_BUG << "AppendIetfAckFrameAndTypeByte failed: " |
| 1085 | << detailed_error(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | break; |
| 1089 | case STOP_WAITING_FRAME: |
| 1090 | set_detailed_error( |
| 1091 | "Attempt to append STOP WAITING frame in version 99."); |
| 1092 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1093 | case MTU_DISCOVERY_FRAME: |
| 1094 | // MTU discovery frames are serialized as ping frames. |
| 1095 | QUIC_FALLTHROUGH_INTENDED; |
| 1096 | case PING_FRAME: |
| 1097 | // Ping has no payload. |
| 1098 | break; |
| 1099 | case RST_STREAM_FRAME: |
| 1100 | if (!AppendRstStreamFrame(*frame.rst_stream_frame, writer)) { |
| 1101 | QUIC_BUG << "AppendRstStreamFrame failed: " << detailed_error(); |
| 1102 | return 0; |
| 1103 | } |
| 1104 | break; |
| 1105 | case CONNECTION_CLOSE_FRAME: |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 1106 | if (!AppendIetfConnectionCloseFrame(*frame.connection_close_frame, |
| 1107 | writer)) { |
| 1108 | QUIC_BUG << "AppendIetfConnectionCloseFrame failed: " |
| 1109 | << detailed_error(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
| 1112 | break; |
| 1113 | case GOAWAY_FRAME: |
| 1114 | set_detailed_error("Attempt to append GOAWAY frame in version 99."); |
| 1115 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 1116 | case WINDOW_UPDATE_FRAME: |
| 1117 | // Depending on whether there is a stream ID or not, will be either a |
| 1118 | // MAX STREAM DATA frame or a MAX DATA frame. |
| 1119 | if (frame.window_update_frame->stream_id == |
| 1120 | QuicUtils::GetInvalidStreamId(transport_version())) { |
| 1121 | if (!AppendMaxDataFrame(*frame.window_update_frame, writer)) { |
| 1122 | QUIC_BUG << "AppendMaxDataFrame failed: " << detailed_error(); |
| 1123 | return 0; |
| 1124 | } |
| 1125 | } else { |
| 1126 | if (!AppendMaxStreamDataFrame(*frame.window_update_frame, writer)) { |
| 1127 | QUIC_BUG << "AppendMaxStreamDataFrame failed: " << detailed_error(); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | } |
| 1131 | break; |
| 1132 | case BLOCKED_FRAME: |
| 1133 | if (!AppendBlockedFrame(*frame.blocked_frame, writer)) { |
| 1134 | QUIC_BUG << "AppendBlockedFrame failed: " << detailed_error(); |
| 1135 | return 0; |
| 1136 | } |
| 1137 | break; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1138 | case MAX_STREAMS_FRAME: |
| 1139 | if (!AppendMaxStreamsFrame(frame.max_streams_frame, writer)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1140 | QUIC_BUG << "AppendMaxStreamsFrame failed" << detailed_error(); |
| 1141 | return 0; |
| 1142 | } |
| 1143 | break; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1144 | case STREAMS_BLOCKED_FRAME: |
| 1145 | if (!AppendStreamsBlockedFrame(frame.streams_blocked_frame, writer)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1146 | QUIC_BUG << "AppendStreamsBlockedFrame failed" << detailed_error(); |
| 1147 | return 0; |
| 1148 | } |
| 1149 | break; |
| 1150 | case NEW_CONNECTION_ID_FRAME: |
| 1151 | if (!AppendNewConnectionIdFrame(*frame.new_connection_id_frame, |
| 1152 | writer)) { |
| 1153 | QUIC_BUG << "AppendNewConnectionIdFrame failed: " << detailed_error(); |
| 1154 | return 0; |
| 1155 | } |
| 1156 | break; |
| 1157 | case RETIRE_CONNECTION_ID_FRAME: |
| 1158 | if (!AppendRetireConnectionIdFrame(*frame.retire_connection_id_frame, |
| 1159 | writer)) { |
| 1160 | QUIC_BUG << "AppendRetireConnectionIdFrame failed: " |
| 1161 | << detailed_error(); |
| 1162 | return 0; |
| 1163 | } |
| 1164 | break; |
| 1165 | case NEW_TOKEN_FRAME: |
| 1166 | if (!AppendNewTokenFrame(*frame.new_token_frame, writer)) { |
| 1167 | QUIC_BUG << "AppendNewTokenFrame failed: " << detailed_error(); |
| 1168 | return 0; |
| 1169 | } |
| 1170 | break; |
| 1171 | case STOP_SENDING_FRAME: |
| 1172 | if (!AppendStopSendingFrame(*frame.stop_sending_frame, writer)) { |
| 1173 | QUIC_BUG << "AppendStopSendingFrame failed: " << detailed_error(); |
| 1174 | return 0; |
| 1175 | } |
| 1176 | break; |
| 1177 | case PATH_CHALLENGE_FRAME: |
| 1178 | if (!AppendPathChallengeFrame(*frame.path_challenge_frame, writer)) { |
| 1179 | QUIC_BUG << "AppendPathChallengeFrame failed: " << detailed_error(); |
| 1180 | return 0; |
| 1181 | } |
| 1182 | break; |
| 1183 | case PATH_RESPONSE_FRAME: |
| 1184 | if (!AppendPathResponseFrame(*frame.path_response_frame, writer)) { |
| 1185 | QUIC_BUG << "AppendPathResponseFrame failed: " << detailed_error(); |
| 1186 | return 0; |
| 1187 | } |
| 1188 | break; |
| 1189 | case MESSAGE_FRAME: |
| 1190 | if (!AppendMessageFrameAndTypeByte(*frame.message_frame, |
| 1191 | last_frame_in_packet, writer)) { |
| 1192 | QUIC_BUG << "AppendMessageFrame failed: " << detailed_error(); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | break; |
| 1196 | case CRYPTO_FRAME: |
| 1197 | if (!AppendCryptoFrame(*frame.crypto_frame, writer)) { |
| 1198 | QUIC_BUG << "AppendCryptoFrame failed: " << detailed_error(); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | break; |
| 1202 | default: |
| 1203 | RaiseError(QUIC_INVALID_FRAME_DATA); |
| 1204 | set_detailed_error("Tried to append unknown frame type."); |
| 1205 | QUIC_BUG << "QUIC_INVALID_FRAME_DATA"; |
| 1206 | return 0; |
| 1207 | } |
| 1208 | ++i; |
| 1209 | } |
| 1210 | |
| 1211 | return writer->length(); |
| 1212 | } |
| 1213 | |
rch | 67cb9df | 2019-03-26 16:52:07 -0700 | [diff] [blame] | 1214 | size_t QuicFramer::BuildConnectivityProbingPacket( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1215 | const QuicPacketHeader& header, |
| 1216 | char* buffer, |
| 1217 | size_t packet_length, |
| 1218 | EncryptionLevel level) { |
| 1219 | QuicFrames frames; |
| 1220 | |
| 1221 | // Write a PING frame, which has no data payload. |
| 1222 | QuicPingFrame ping_frame; |
| 1223 | frames.push_back(QuicFrame(ping_frame)); |
| 1224 | |
| 1225 | // Add padding to the rest of the packet. |
| 1226 | QuicPaddingFrame padding_frame; |
| 1227 | frames.push_back(QuicFrame(padding_frame)); |
| 1228 | |
| 1229 | return BuildDataPacket(header, frames, buffer, packet_length, level); |
| 1230 | } |
| 1231 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1232 | size_t QuicFramer::BuildPaddedPathChallengePacket( |
| 1233 | const QuicPacketHeader& header, |
| 1234 | char* buffer, |
| 1235 | size_t packet_length, |
| 1236 | QuicPathFrameBuffer* payload, |
| 1237 | QuicRandom* randomizer, |
| 1238 | EncryptionLevel level) { |
| 1239 | if (version_.transport_version != QUIC_VERSION_99) { |
| 1240 | QUIC_BUG << "Attempt to build a PATH_CHALLENGE Connectivity Probing " |
| 1241 | "packet and not doing IETF QUIC"; |
| 1242 | return 0; |
| 1243 | } |
| 1244 | QuicFrames frames; |
| 1245 | |
| 1246 | // Write a PATH_CHALLENGE frame, which has a random 8-byte payload |
| 1247 | randomizer->RandBytes(payload->data(), payload->size()); |
| 1248 | |
| 1249 | QuicPathChallengeFrame path_challenge_frame(0, *payload); |
| 1250 | frames.push_back(QuicFrame(&path_challenge_frame)); |
| 1251 | |
| 1252 | // Add padding to the rest of the packet in order to assess Path MTU |
| 1253 | // characteristics. |
| 1254 | QuicPaddingFrame padding_frame; |
| 1255 | frames.push_back(QuicFrame(padding_frame)); |
| 1256 | |
| 1257 | return BuildDataPacket(header, frames, buffer, packet_length, level); |
| 1258 | } |
| 1259 | |
| 1260 | size_t QuicFramer::BuildPathResponsePacket( |
| 1261 | const QuicPacketHeader& header, |
| 1262 | char* buffer, |
| 1263 | size_t packet_length, |
| 1264 | const QuicDeque<QuicPathFrameBuffer>& payloads, |
| 1265 | const bool is_padded, |
| 1266 | EncryptionLevel level) { |
| 1267 | if (payloads.empty()) { |
| 1268 | QUIC_BUG |
| 1269 | << "Attempt to generate connectivity response with no request payloads"; |
| 1270 | return 0; |
| 1271 | } |
| 1272 | if (version_.transport_version != QUIC_VERSION_99) { |
| 1273 | QUIC_BUG << "Attempt to build a PATH_RESPONSE Connectivity Probing " |
| 1274 | "packet and not doing IETF QUIC"; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | std::vector<std::unique_ptr<QuicPathResponseFrame>> path_response_frames; |
| 1279 | for (const QuicPathFrameBuffer& payload : payloads) { |
| 1280 | // Note that the control frame ID can be 0 since this is not retransmitted. |
| 1281 | path_response_frames.push_back( |
| 1282 | QuicMakeUnique<QuicPathResponseFrame>(0, payload)); |
| 1283 | } |
| 1284 | |
| 1285 | QuicFrames frames; |
| 1286 | for (const std::unique_ptr<QuicPathResponseFrame>& path_response_frame : |
| 1287 | path_response_frames) { |
| 1288 | frames.push_back(QuicFrame(path_response_frame.get())); |
| 1289 | } |
| 1290 | |
| 1291 | if (is_padded) { |
| 1292 | // Add padding to the rest of the packet in order to assess Path MTU |
| 1293 | // characteristics. |
| 1294 | QuicPaddingFrame padding_frame; |
| 1295 | frames.push_back(QuicFrame(padding_frame)); |
| 1296 | } |
| 1297 | |
| 1298 | return BuildDataPacket(header, frames, buffer, packet_length, level); |
| 1299 | } |
| 1300 | |
| 1301 | // static |
| 1302 | std::unique_ptr<QuicEncryptedPacket> QuicFramer::BuildPublicResetPacket( |
| 1303 | const QuicPublicResetPacket& packet) { |
| 1304 | CryptoHandshakeMessage reset; |
| 1305 | reset.set_tag(kPRST); |
| 1306 | reset.SetValue(kRNON, packet.nonce_proof); |
| 1307 | if (packet.client_address.host().address_family() != |
| 1308 | IpAddressFamily::IP_UNSPEC) { |
| 1309 | // packet.client_address is non-empty. |
| 1310 | QuicSocketAddressCoder address_coder(packet.client_address); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1311 | std::string serialized_address = address_coder.Encode(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1312 | if (serialized_address.empty()) { |
| 1313 | return nullptr; |
| 1314 | } |
| 1315 | reset.SetStringPiece(kCADR, serialized_address); |
| 1316 | } |
| 1317 | if (!packet.endpoint_id.empty()) { |
| 1318 | reset.SetStringPiece(kEPID, packet.endpoint_id); |
| 1319 | } |
| 1320 | const QuicData& reset_serialized = reset.GetSerialized(); |
| 1321 | |
| 1322 | size_t len = kPublicFlagsSize + packet.connection_id.length() + |
| 1323 | reset_serialized.length(); |
| 1324 | std::unique_ptr<char[]> buffer(new char[len]); |
| 1325 | // Endianness is not a concern here, as writer is not going to write integers |
| 1326 | // or floating numbers. |
| 1327 | QuicDataWriter writer(len, buffer.get()); |
| 1328 | |
| 1329 | uint8_t flags = static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_RST | |
| 1330 | PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID); |
| 1331 | // This hack makes post-v33 public reset packet look like pre-v33 packets. |
| 1332 | flags |= static_cast<uint8_t>(PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); |
| 1333 | if (!writer.WriteUInt8(flags)) { |
| 1334 | return nullptr; |
| 1335 | } |
| 1336 | |
| 1337 | if (!writer.WriteConnectionId(packet.connection_id)) { |
| 1338 | return nullptr; |
| 1339 | } |
| 1340 | |
| 1341 | if (!writer.WriteBytes(reset_serialized.data(), reset_serialized.length())) { |
| 1342 | return nullptr; |
| 1343 | } |
| 1344 | |
| 1345 | return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); |
| 1346 | } |
| 1347 | |
| 1348 | // static |
| 1349 | std::unique_ptr<QuicEncryptedPacket> QuicFramer::BuildIetfStatelessResetPacket( |
| 1350 | QuicConnectionId connection_id, |
| 1351 | QuicUint128 stateless_reset_token) { |
| 1352 | QUIC_DVLOG(1) << "Building IETF stateless reset packet."; |
| 1353 | size_t len = kPacketHeaderTypeSize + kMinRandomBytesLengthInStatelessReset + |
| 1354 | sizeof(stateless_reset_token); |
| 1355 | std::unique_ptr<char[]> buffer(new char[len]); |
| 1356 | QuicDataWriter writer(len, buffer.get()); |
| 1357 | |
| 1358 | uint8_t type = 0; |
| 1359 | type |= FLAGS_FIXED_BIT; |
| 1360 | type |= FLAGS_SHORT_HEADER_RESERVED_1; |
| 1361 | type |= FLAGS_SHORT_HEADER_RESERVED_2; |
| 1362 | type |= PacketNumberLengthToOnWireValue(QUIC_VERSION_UNSUPPORTED, |
| 1363 | PACKET_1BYTE_PACKET_NUMBER); |
| 1364 | |
| 1365 | // Append type byte. |
| 1366 | if (!writer.WriteUInt8(type)) { |
| 1367 | return nullptr; |
| 1368 | } |
| 1369 | // Append random bytes. |
| 1370 | if (!writer.WriteRandomBytes(QuicRandom::GetInstance(), |
| 1371 | kMinRandomBytesLengthInStatelessReset)) { |
| 1372 | return nullptr; |
| 1373 | } |
| 1374 | |
| 1375 | // Append stateless reset token. |
| 1376 | if (!writer.WriteBytes(&stateless_reset_token, |
| 1377 | sizeof(stateless_reset_token))) { |
| 1378 | return nullptr; |
| 1379 | } |
| 1380 | return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); |
| 1381 | } |
| 1382 | |
| 1383 | // static |
| 1384 | std::unique_ptr<QuicEncryptedPacket> QuicFramer::BuildVersionNegotiationPacket( |
| 1385 | QuicConnectionId connection_id, |
| 1386 | bool ietf_quic, |
| 1387 | const ParsedQuicVersionVector& versions) { |
| 1388 | if (ietf_quic) { |
| 1389 | return BuildIetfVersionNegotiationPacket(connection_id, versions); |
| 1390 | } |
| 1391 | DCHECK(!versions.empty()); |
| 1392 | size_t len = kPublicFlagsSize + connection_id.length() + |
| 1393 | versions.size() * kQuicVersionSize; |
| 1394 | std::unique_ptr<char[]> buffer(new char[len]); |
| 1395 | // Endianness is not a concern here, version negotiation packet does not have |
| 1396 | // integers or floating numbers. |
| 1397 | QuicDataWriter writer(len, buffer.get()); |
| 1398 | |
| 1399 | uint8_t flags = static_cast<uint8_t>( |
| 1400 | PACKET_PUBLIC_FLAGS_VERSION | PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID | |
| 1401 | // TODO(rch): Remove this QUIC_VERSION_32 is retired. |
| 1402 | PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD); |
| 1403 | if (!writer.WriteUInt8(flags)) { |
| 1404 | return nullptr; |
| 1405 | } |
| 1406 | |
| 1407 | if (!writer.WriteConnectionId(connection_id)) { |
| 1408 | return nullptr; |
| 1409 | } |
| 1410 | |
| 1411 | for (const ParsedQuicVersion& version : versions) { |
| 1412 | // TODO(rch): Use WriteUInt32() once QUIC_VERSION_35 is removed. |
| 1413 | if (!writer.WriteTag( |
| 1414 | QuicEndian::HostToNet32(CreateQuicVersionLabel(version)))) { |
| 1415 | return nullptr; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); |
| 1420 | } |
| 1421 | |
| 1422 | // static |
| 1423 | std::unique_ptr<QuicEncryptedPacket> |
| 1424 | QuicFramer::BuildIetfVersionNegotiationPacket( |
| 1425 | QuicConnectionId connection_id, |
| 1426 | const ParsedQuicVersionVector& versions) { |
| 1427 | QUIC_DVLOG(1) << "Building IETF version negotiation packet."; |
| 1428 | DCHECK(!versions.empty()); |
| 1429 | size_t len = kPacketHeaderTypeSize + kConnectionIdLengthSize + |
| 1430 | connection_id.length() + |
| 1431 | (versions.size() + 1) * kQuicVersionSize; |
| 1432 | std::unique_ptr<char[]> buffer(new char[len]); |
| 1433 | QuicDataWriter writer(len, buffer.get()); |
| 1434 | |
| 1435 | // TODO(fayang): Randomly select a value for the type. |
dschinazi | 072da7c | 2019-05-07 17:57:42 -0700 | [diff] [blame] | 1436 | uint8_t type = static_cast<uint8_t>(FLAGS_LONG_HEADER); |
dschinazi | 9e92fb3 | 2019-05-08 14:47:24 -0700 | [diff] [blame] | 1437 | if (GetQuicReloadableFlag(quic_send_version_negotiation_fixed_bit)) { |
| 1438 | QUIC_RELOADABLE_FLAG_COUNT(quic_send_version_negotiation_fixed_bit); |
| 1439 | type |= static_cast<uint8_t>(FLAGS_FIXED_BIT); |
| 1440 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1441 | if (!writer.WriteUInt8(type)) { |
| 1442 | return nullptr; |
| 1443 | } |
| 1444 | |
| 1445 | if (!writer.WriteUInt32(0)) { |
| 1446 | return nullptr; |
| 1447 | } |
| 1448 | |
| 1449 | if (!GetQuicReloadableFlag(quic_use_new_append_connection_id)) { |
| 1450 | if (!AppendIetfConnectionId(true, EmptyQuicConnectionId(), |
| 1451 | PACKET_0BYTE_CONNECTION_ID, connection_id, |
| 1452 | PACKET_8BYTE_CONNECTION_ID, &writer)) { |
| 1453 | return nullptr; |
| 1454 | } |
| 1455 | } else { |
| 1456 | QUIC_RELOADABLE_FLAG_COUNT_N(quic_use_new_append_connection_id, 1, 2); |
| 1457 | if (!AppendIetfConnectionIdsNew(true, EmptyQuicConnectionId(), |
| 1458 | connection_id, &writer)) { |
| 1459 | return nullptr; |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | for (const ParsedQuicVersion& version : versions) { |
| 1464 | // TODO(rch): Use WriteUInt32() once QUIC_VERSION_35 is removed. |
| 1465 | if (!writer.WriteTag( |
| 1466 | QuicEndian::HostToNet32(CreateQuicVersionLabel(version)))) { |
| 1467 | return nullptr; |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | return QuicMakeUnique<QuicEncryptedPacket>(buffer.release(), len, true); |
| 1472 | } |
| 1473 | |
| 1474 | bool QuicFramer::ProcessPacket(const QuicEncryptedPacket& packet) { |
| 1475 | QuicDataReader reader(packet.data(), packet.length()); |
| 1476 | |
| 1477 | bool packet_has_ietf_packet_header = false; |
| 1478 | if (infer_packet_header_type_from_version_) { |
| 1479 | packet_has_ietf_packet_header = |
| 1480 | version_.transport_version > QUIC_VERSION_43; |
| 1481 | } else if (!reader.IsDoneReading()) { |
| 1482 | uint8_t type = reader.PeekByte(); |
| 1483 | packet_has_ietf_packet_header = QuicUtils::IsIetfPacketHeader(type); |
| 1484 | } |
| 1485 | if (packet_has_ietf_packet_header) { |
| 1486 | QUIC_DVLOG(1) << ENDPOINT << "Processing IETF QUIC packet."; |
| 1487 | } |
| 1488 | |
| 1489 | visitor_->OnPacket(); |
| 1490 | |
| 1491 | QuicPacketHeader header; |
| 1492 | if (!ProcessPublicHeader(&reader, packet_has_ietf_packet_header, &header)) { |
| 1493 | DCHECK_NE("", detailed_error_); |
| 1494 | QUIC_DVLOG(1) << ENDPOINT << "Unable to process public header. Error: " |
| 1495 | << detailed_error_; |
| 1496 | DCHECK_NE("", detailed_error_); |
| 1497 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PUBLIC_HEADER); |
| 1498 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1499 | } |
| 1500 | |
| 1501 | if (!visitor_->OnUnauthenticatedPublicHeader(header)) { |
| 1502 | // The visitor suppresses further processing of the packet. |
| 1503 | return true; |
| 1504 | } |
| 1505 | |
dschinazi | e0df3f7 | 2019-05-06 16:37:51 -0700 | [diff] [blame] | 1506 | if (IsVersionNegotiation(header, packet_has_ietf_packet_header)) { |
dschinazi | 072da7c | 2019-05-07 17:57:42 -0700 | [diff] [blame] | 1507 | if (!GetQuicRestartFlag(quic_server_drop_version_negotiation)) { |
| 1508 | QUIC_DVLOG(1) << ENDPOINT << "Received version negotiation packet"; |
| 1509 | return ProcessVersionNegotiationPacket(&reader, header); |
| 1510 | } |
| 1511 | QUIC_RESTART_FLAG_COUNT_N(quic_server_drop_version_negotiation, 1, 2); |
| 1512 | if (perspective_ == Perspective::IS_CLIENT) { |
| 1513 | QUIC_DVLOG(1) << "Client received version negotiation packet"; |
| 1514 | return ProcessVersionNegotiationPacket(&reader, header); |
| 1515 | } else { |
| 1516 | QUIC_DLOG(ERROR) << "Server received version negotiation packet"; |
| 1517 | set_detailed_error("Server received version negotiation packet."); |
| 1518 | return RaiseError(QUIC_INVALID_VERSION_NEGOTIATION_PACKET); |
| 1519 | } |
dschinazi | e0df3f7 | 2019-05-06 16:37:51 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | if (header.version_flag && header.version != version_) { |
| 1523 | if (perspective_ == Perspective::IS_SERVER) { |
| 1524 | if (!visitor_->OnProtocolVersionMismatch(header.version, header.form)) { |
| 1525 | RecordDroppedPacketReason(DroppedPacketReason::VERSION_MISMATCH); |
| 1526 | return true; |
| 1527 | } |
| 1528 | } else { |
| 1529 | // A client received a packet of a different version but that packet is |
| 1530 | // not a version negotiation packet. It is therefore invalid and dropped. |
| 1531 | QUIC_DLOG(ERROR) << "Client received unexpected version " |
| 1532 | << ParsedQuicVersionToString(header.version) |
| 1533 | << " instead of " << ParsedQuicVersionToString(version_); |
| 1534 | set_detailed_error("Client received unexpected version."); |
| 1535 | return RaiseError(QUIC_INVALID_VERSION); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | bool rv; |
dschinazi | e0df3f7 | 2019-05-06 16:37:51 -0700 | [diff] [blame] | 1540 | if (header.long_packet_type == RETRY) { |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 1541 | rv = ProcessRetryPacket(&reader, header); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1542 | } else if (header.reset_flag) { |
| 1543 | rv = ProcessPublicResetPacket(&reader, header); |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 1544 | } else if (packet.length() <= kMaxIncomingPacketSize) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1545 | // The optimized decryption algorithm implementations run faster when |
| 1546 | // operating on aligned memory. |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 1547 | QUIC_CACHELINE_ALIGNED char buffer[kMaxIncomingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1548 | if (packet_has_ietf_packet_header) { |
| 1549 | rv = ProcessIetfDataPacket(&reader, &header, packet, buffer, |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 1550 | QUIC_ARRAYSIZE(buffer)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1551 | } else { |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 1552 | rv = ProcessDataPacket(&reader, &header, packet, buffer, |
| 1553 | QUIC_ARRAYSIZE(buffer)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1554 | } |
| 1555 | } else { |
| 1556 | std::unique_ptr<char[]> large_buffer(new char[packet.length()]); |
| 1557 | if (packet_has_ietf_packet_header) { |
| 1558 | rv = ProcessIetfDataPacket(&reader, &header, packet, large_buffer.get(), |
| 1559 | packet.length()); |
| 1560 | } else { |
| 1561 | rv = ProcessDataPacket(&reader, &header, packet, large_buffer.get(), |
| 1562 | packet.length()); |
| 1563 | } |
| 1564 | QUIC_BUG_IF(rv) << "QUIC should never successfully process packets larger" |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 1565 | << "than kMaxIncomingPacketSize. packet size:" |
| 1566 | << packet.length(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1567 | } |
| 1568 | return rv; |
| 1569 | } |
| 1570 | |
| 1571 | bool QuicFramer::ProcessVersionNegotiationPacket( |
| 1572 | QuicDataReader* reader, |
| 1573 | const QuicPacketHeader& header) { |
| 1574 | DCHECK_EQ(Perspective::IS_CLIENT, perspective_); |
| 1575 | |
| 1576 | QuicVersionNegotiationPacket packet(header.destination_connection_id); |
| 1577 | // Try reading at least once to raise error if the packet is invalid. |
| 1578 | do { |
| 1579 | QuicVersionLabel version_label; |
fayang | 4031554 | 2019-05-09 09:19:09 -0700 | [diff] [blame^] | 1580 | if (!ProcessVersionLabel(reader, &version_label)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1581 | set_detailed_error("Unable to read supported version in negotiation."); |
| 1582 | RecordDroppedPacketReason( |
| 1583 | DroppedPacketReason::INVALID_VERSION_NEGOTIATION_PACKET); |
| 1584 | return RaiseError(QUIC_INVALID_VERSION_NEGOTIATION_PACKET); |
| 1585 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1586 | packet.versions.push_back(ParseQuicVersionLabel(version_label)); |
| 1587 | } while (!reader->IsDoneReading()); |
| 1588 | |
| 1589 | visitor_->OnVersionNegotiationPacket(packet); |
| 1590 | return true; |
| 1591 | } |
| 1592 | |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 1593 | bool QuicFramer::ProcessRetryPacket(QuicDataReader* reader, |
| 1594 | const QuicPacketHeader& header) { |
| 1595 | DCHECK_EQ(Perspective::IS_CLIENT, perspective_); |
| 1596 | |
| 1597 | // Parse Original Destination Connection ID Length. |
| 1598 | uint8_t odcil = header.type_byte & 0xf; |
| 1599 | if (odcil != 0) { |
| 1600 | odcil += kConnectionIdLengthAdjustment; |
| 1601 | } |
| 1602 | |
| 1603 | // Parse Original Destination Connection ID. |
| 1604 | QuicConnectionId original_destination_connection_id; |
| 1605 | if (!reader->ReadConnectionId(&original_destination_connection_id, odcil)) { |
| 1606 | set_detailed_error("Unable to read Original Destination ConnectionId."); |
| 1607 | return false; |
| 1608 | } |
| 1609 | |
| 1610 | QuicStringPiece retry_token = reader->ReadRemainingPayload(); |
| 1611 | visitor_->OnRetryPacket(original_destination_connection_id, |
| 1612 | header.source_connection_id, retry_token); |
| 1613 | return true; |
| 1614 | } |
| 1615 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1616 | bool QuicFramer::MaybeProcessIetfInitialRetryToken( |
| 1617 | QuicDataReader* encrypted_reader, |
| 1618 | QuicPacketHeader* header) { |
| 1619 | if (!QuicVersionHasLongHeaderLengths(header->version.transport_version) || |
| 1620 | header->form != IETF_QUIC_LONG_HEADER_PACKET || |
| 1621 | header->long_packet_type != INITIAL) { |
| 1622 | return true; |
| 1623 | } |
| 1624 | uint64_t retry_token_length = 0; |
| 1625 | header->retry_token_length_length = encrypted_reader->PeekVarInt62Length(); |
| 1626 | if (!encrypted_reader->ReadVarInt62(&retry_token_length)) { |
| 1627 | set_detailed_error("Unable to read INITIAL retry token length."); |
| 1628 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1629 | } |
| 1630 | header->retry_token = encrypted_reader->PeekRemainingPayload(); |
| 1631 | // Safety check to avoid spending ressources if malformed. |
| 1632 | // At this point header->retry_token contains the rest of the packet |
| 1633 | // so its length() is the amount of data remaining in the packet. |
| 1634 | if (retry_token_length > header->retry_token.length()) { |
| 1635 | set_detailed_error("INITIAL token length longer than packet."); |
| 1636 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1637 | } |
| 1638 | // Resize retry_token to make it only contain the retry token. |
| 1639 | header->retry_token.remove_suffix(header->retry_token.length() - |
| 1640 | retry_token_length); |
| 1641 | // Advance encrypted_reader by retry_token_length. |
| 1642 | uint8_t wasted_byte; |
| 1643 | for (uint64_t i = 0; i < retry_token_length; ++i) { |
| 1644 | if (!encrypted_reader->ReadUInt8(&wasted_byte)) { |
| 1645 | set_detailed_error("Unable to read INITIAL retry token."); |
| 1646 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1647 | } |
| 1648 | } |
| 1649 | return true; |
| 1650 | } |
| 1651 | |
| 1652 | // Seeks the current packet to check for a coalesced packet at the end. |
| 1653 | // If the IETF length field only spans part of the outer packet, |
| 1654 | // then there is a coalesced packet after this one. |
| 1655 | void QuicFramer::MaybeProcessCoalescedPacket( |
| 1656 | const QuicDataReader& encrypted_reader, |
| 1657 | uint64_t remaining_bytes_length, |
| 1658 | const QuicPacketHeader& header) { |
| 1659 | if (header.remaining_packet_length >= remaining_bytes_length) { |
| 1660 | // There is no coalesced packet. |
| 1661 | return; |
| 1662 | } |
| 1663 | |
| 1664 | QuicStringPiece remaining_data = encrypted_reader.PeekRemainingPayload(); |
| 1665 | DCHECK_EQ(remaining_data.length(), remaining_bytes_length); |
| 1666 | |
| 1667 | const char* coalesced_data = |
| 1668 | remaining_data.data() + header.remaining_packet_length; |
| 1669 | uint64_t coalesced_data_length = |
| 1670 | remaining_bytes_length - header.remaining_packet_length; |
| 1671 | QuicDataReader coalesced_reader(coalesced_data, coalesced_data_length); |
| 1672 | |
| 1673 | QuicPacketHeader coalesced_header; |
| 1674 | if (!ProcessIetfPacketHeader(&coalesced_reader, &coalesced_header)) { |
| 1675 | QUIC_PEER_BUG << ENDPOINT |
| 1676 | << "Failed to parse received coalesced header of length " |
| 1677 | << coalesced_data_length << ": " |
| 1678 | << QuicTextUtils::HexEncode(coalesced_data, |
| 1679 | coalesced_data_length) |
| 1680 | << " previous header was " << header; |
| 1681 | return; |
| 1682 | } |
| 1683 | |
| 1684 | if (coalesced_header.destination_connection_id != |
| 1685 | header.destination_connection_id || |
| 1686 | (coalesced_header.form != IETF_QUIC_SHORT_HEADER_PACKET && |
| 1687 | coalesced_header.version != header.version)) { |
| 1688 | QUIC_PEER_BUG << ENDPOINT << "Received mismatched coalesced header " |
| 1689 | << coalesced_header << " previous header was " << header; |
| 1690 | return; |
| 1691 | } |
| 1692 | |
| 1693 | QuicEncryptedPacket coalesced_packet(coalesced_data, coalesced_data_length, |
| 1694 | /*owns_buffer=*/false); |
| 1695 | visitor_->OnCoalescedPacket(coalesced_packet); |
| 1696 | } |
| 1697 | |
| 1698 | bool QuicFramer::MaybeProcessIetfLength(QuicDataReader* encrypted_reader, |
| 1699 | QuicPacketHeader* header) { |
| 1700 | if (!QuicVersionHasLongHeaderLengths(header->version.transport_version) || |
| 1701 | header->form != IETF_QUIC_LONG_HEADER_PACKET || |
| 1702 | (header->long_packet_type != INITIAL && |
| 1703 | header->long_packet_type != HANDSHAKE && |
| 1704 | header->long_packet_type != ZERO_RTT_PROTECTED)) { |
| 1705 | return true; |
| 1706 | } |
| 1707 | header->length_length = encrypted_reader->PeekVarInt62Length(); |
| 1708 | if (!encrypted_reader->ReadVarInt62(&header->remaining_packet_length)) { |
| 1709 | set_detailed_error("Unable to read long header payload length."); |
| 1710 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1711 | } |
| 1712 | uint64_t remaining_bytes_length = encrypted_reader->BytesRemaining(); |
| 1713 | if (header->remaining_packet_length > remaining_bytes_length) { |
| 1714 | set_detailed_error("Long header payload length longer than packet."); |
| 1715 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1716 | } |
| 1717 | |
| 1718 | MaybeProcessCoalescedPacket(*encrypted_reader, remaining_bytes_length, |
| 1719 | *header); |
| 1720 | |
| 1721 | if (!encrypted_reader->TruncateRemaining(header->remaining_packet_length)) { |
| 1722 | set_detailed_error("Length TruncateRemaining failed."); |
| 1723 | QUIC_BUG << "Length TruncateRemaining failed."; |
| 1724 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1725 | } |
| 1726 | return true; |
| 1727 | } |
| 1728 | |
| 1729 | bool QuicFramer::ProcessIetfDataPacket(QuicDataReader* encrypted_reader, |
| 1730 | QuicPacketHeader* header, |
| 1731 | const QuicEncryptedPacket& packet, |
| 1732 | char* decrypted_buffer, |
| 1733 | size_t buffer_length) { |
| 1734 | DCHECK_NE(GOOGLE_QUIC_PACKET, header->form); |
| 1735 | DCHECK(!header->has_possible_stateless_reset_token); |
| 1736 | header->retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_0; |
| 1737 | header->retry_token = QuicStringPiece(); |
| 1738 | header->length_length = VARIABLE_LENGTH_INTEGER_LENGTH_0; |
| 1739 | header->remaining_packet_length = 0; |
| 1740 | if (header->form == IETF_QUIC_SHORT_HEADER_PACKET && |
| 1741 | perspective_ == Perspective::IS_CLIENT) { |
| 1742 | // Peek possible stateless reset token. Will only be used on decryption |
| 1743 | // failure. |
| 1744 | QuicStringPiece remaining = encrypted_reader->PeekRemainingPayload(); |
| 1745 | if (remaining.length() >= sizeof(header->possible_stateless_reset_token)) { |
| 1746 | header->has_possible_stateless_reset_token = true; |
| 1747 | memcpy(&header->possible_stateless_reset_token, |
| 1748 | &remaining.data()[remaining.length() - |
| 1749 | sizeof(header->possible_stateless_reset_token)], |
| 1750 | sizeof(header->possible_stateless_reset_token)); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | if (!MaybeProcessIetfInitialRetryToken(encrypted_reader, header)) { |
| 1755 | return false; |
| 1756 | } |
| 1757 | |
| 1758 | if (!MaybeProcessIetfLength(encrypted_reader, header)) { |
| 1759 | return false; |
| 1760 | } |
| 1761 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1762 | QuicStringPiece associated_data; |
| 1763 | std::vector<char> ad_storage; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1764 | if (header->form == IETF_QUIC_SHORT_HEADER_PACKET || |
| 1765 | header->long_packet_type != VERSION_NEGOTIATION) { |
dschinazi | 072da7c | 2019-05-07 17:57:42 -0700 | [diff] [blame] | 1766 | DCHECK(header->form == IETF_QUIC_SHORT_HEADER_PACKET || |
| 1767 | header->long_packet_type == INITIAL || |
| 1768 | header->long_packet_type == HANDSHAKE || |
| 1769 | header->long_packet_type == ZERO_RTT_PROTECTED); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1770 | // Process packet number. |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1771 | QuicPacketNumber base_packet_number; |
| 1772 | if (supports_multiple_packet_number_spaces_) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1773 | PacketNumberSpace pn_space = GetPacketNumberSpace(*header); |
| 1774 | if (pn_space == NUM_PACKET_NUMBER_SPACES) { |
| 1775 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1776 | } |
| 1777 | base_packet_number = largest_decrypted_packet_numbers_[pn_space]; |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1778 | } else { |
| 1779 | base_packet_number = largest_packet_number_; |
| 1780 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1781 | uint64_t full_packet_number; |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1782 | bool hp_removal_failed = false; |
| 1783 | if (version_.HasHeaderProtection()) { |
| 1784 | if (!RemoveHeaderProtection(encrypted_reader, packet, header, |
| 1785 | &full_packet_number, &ad_storage)) { |
| 1786 | hp_removal_failed = true; |
| 1787 | } |
| 1788 | associated_data = QuicStringPiece(ad_storage.data(), ad_storage.size()); |
| 1789 | } else if (!ProcessAndCalculatePacketNumber( |
| 1790 | encrypted_reader, header->packet_number_length, |
| 1791 | base_packet_number, &full_packet_number)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1792 | set_detailed_error("Unable to read packet number."); |
| 1793 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PACKET_NUMBER); |
| 1794 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1795 | } |
| 1796 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1797 | if (hp_removal_failed || |
| 1798 | !IsValidFullPacketNumber(full_packet_number, transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1799 | if (IsIetfStatelessResetPacket(*header)) { |
| 1800 | // This is a stateless reset packet. |
| 1801 | QuicIetfStatelessResetPacket packet( |
| 1802 | *header, header->possible_stateless_reset_token); |
| 1803 | visitor_->OnAuthenticatedIetfStatelessResetPacket(packet); |
| 1804 | return true; |
| 1805 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1806 | if (hp_removal_failed) { |
| 1807 | set_detailed_error("Unable to decrypt header protection."); |
| 1808 | return RaiseError(QUIC_DECRYPTION_FAILURE); |
| 1809 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1810 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PACKET_NUMBER); |
| 1811 | set_detailed_error("packet numbers cannot be 0."); |
| 1812 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1813 | } |
| 1814 | header->packet_number = QuicPacketNumber(full_packet_number); |
| 1815 | } |
| 1816 | |
| 1817 | // A nonce should only present in SHLO from the server to the client when |
| 1818 | // using QUIC crypto. |
| 1819 | if (header->form == IETF_QUIC_LONG_HEADER_PACKET && |
| 1820 | header->long_packet_type == ZERO_RTT_PROTECTED && |
| 1821 | perspective_ == Perspective::IS_CLIENT && |
| 1822 | version_.handshake_protocol == PROTOCOL_QUIC_CRYPTO) { |
| 1823 | if (!encrypted_reader->ReadBytes( |
| 1824 | reinterpret_cast<uint8_t*>(last_nonce_.data()), |
| 1825 | last_nonce_.size())) { |
| 1826 | set_detailed_error("Unable to read nonce."); |
| 1827 | RecordDroppedPacketReason( |
| 1828 | DroppedPacketReason::INVALID_DIVERSIFICATION_NONCE); |
| 1829 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 1830 | } |
| 1831 | |
| 1832 | header->nonce = &last_nonce_; |
| 1833 | } else { |
| 1834 | header->nonce = nullptr; |
| 1835 | } |
| 1836 | |
| 1837 | if (!visitor_->OnUnauthenticatedHeader(*header)) { |
| 1838 | set_detailed_error( |
| 1839 | "Visitor asked to stop processing of unauthenticated header."); |
| 1840 | return false; |
| 1841 | } |
| 1842 | |
| 1843 | QuicStringPiece encrypted = encrypted_reader->ReadRemainingPayload(); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 1844 | if (!version_.HasHeaderProtection()) { |
| 1845 | associated_data = GetAssociatedDataFromEncryptedPacket( |
| 1846 | version_.transport_version, packet, |
| 1847 | GetIncludedDestinationConnectionIdLength(*header), |
| 1848 | GetIncludedSourceConnectionIdLength(*header), header->version_flag, |
| 1849 | header->nonce != nullptr, header->packet_number_length, |
| 1850 | header->retry_token_length_length, header->retry_token.length(), |
| 1851 | header->length_length); |
| 1852 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1853 | |
| 1854 | size_t decrypted_length = 0; |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1855 | EncryptionLevel decrypted_level; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1856 | if (!DecryptPayload(encrypted, associated_data, *header, decrypted_buffer, |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1857 | buffer_length, &decrypted_length, &decrypted_level)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1858 | if (IsIetfStatelessResetPacket(*header)) { |
| 1859 | // This is a stateless reset packet. |
| 1860 | QuicIetfStatelessResetPacket packet( |
| 1861 | *header, header->possible_stateless_reset_token); |
| 1862 | visitor_->OnAuthenticatedIetfStatelessResetPacket(packet); |
| 1863 | return true; |
| 1864 | } |
| 1865 | set_detailed_error("Unable to decrypt payload."); |
| 1866 | RecordDroppedPacketReason(DroppedPacketReason::DECRYPTION_FAILURE); |
| 1867 | return RaiseError(QUIC_DECRYPTION_FAILURE); |
| 1868 | } |
| 1869 | QuicDataReader reader(decrypted_buffer, decrypted_length); |
| 1870 | |
| 1871 | // Update the largest packet number after we have decrypted the packet |
| 1872 | // so we are confident is not attacker controlled. |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1873 | if (supports_multiple_packet_number_spaces_) { |
| 1874 | largest_decrypted_packet_numbers_[QuicUtils::GetPacketNumberSpace( |
| 1875 | decrypted_level)] |
| 1876 | .UpdateMax(header->packet_number); |
| 1877 | } else { |
| 1878 | largest_packet_number_.UpdateMax(header->packet_number); |
| 1879 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1880 | |
| 1881 | if (!visitor_->OnPacketHeader(*header)) { |
| 1882 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PACKET_NUMBER); |
| 1883 | // The visitor suppresses further processing of the packet. |
| 1884 | return true; |
| 1885 | } |
| 1886 | |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 1887 | if (packet.length() > kMaxIncomingPacketSize) { |
| 1888 | set_detailed_error("Packet too large."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1889 | return RaiseError(QUIC_PACKET_TOO_LARGE); |
| 1890 | } |
| 1891 | |
| 1892 | // Handle the payload. |
| 1893 | if (version_.transport_version == QUIC_VERSION_99) { |
| 1894 | if (!ProcessIetfFrameData(&reader, *header)) { |
| 1895 | DCHECK_NE(QUIC_NO_ERROR, error_); // ProcessIetfFrameData sets the error. |
| 1896 | DCHECK_NE("", detailed_error_); |
| 1897 | QUIC_DLOG(WARNING) << ENDPOINT << "Unable to process frame data. Error: " |
| 1898 | << detailed_error_; |
| 1899 | return false; |
| 1900 | } |
| 1901 | } else { |
| 1902 | if (!ProcessFrameData(&reader, *header)) { |
| 1903 | DCHECK_NE(QUIC_NO_ERROR, error_); // ProcessFrameData sets the error. |
| 1904 | DCHECK_NE("", detailed_error_); |
| 1905 | QUIC_DLOG(WARNING) << ENDPOINT << "Unable to process frame data. Error: " |
| 1906 | << detailed_error_; |
| 1907 | return false; |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | visitor_->OnPacketComplete(); |
| 1912 | return true; |
| 1913 | } |
| 1914 | |
| 1915 | bool QuicFramer::ProcessDataPacket(QuicDataReader* encrypted_reader, |
| 1916 | QuicPacketHeader* header, |
| 1917 | const QuicEncryptedPacket& packet, |
| 1918 | char* decrypted_buffer, |
| 1919 | size_t buffer_length) { |
| 1920 | if (!ProcessUnauthenticatedHeader(encrypted_reader, header)) { |
| 1921 | DCHECK_NE("", detailed_error_); |
| 1922 | QUIC_DVLOG(1) |
| 1923 | << ENDPOINT |
| 1924 | << "Unable to process packet header. Stopping parsing. Error: " |
| 1925 | << detailed_error_; |
| 1926 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PACKET_NUMBER); |
| 1927 | return false; |
| 1928 | } |
| 1929 | |
| 1930 | QuicStringPiece encrypted = encrypted_reader->ReadRemainingPayload(); |
| 1931 | QuicStringPiece associated_data = GetAssociatedDataFromEncryptedPacket( |
| 1932 | version_.transport_version, packet, |
| 1933 | GetIncludedDestinationConnectionIdLength(*header), |
| 1934 | GetIncludedSourceConnectionIdLength(*header), header->version_flag, |
| 1935 | header->nonce != nullptr, header->packet_number_length, |
| 1936 | header->retry_token_length_length, header->retry_token.length(), |
| 1937 | header->length_length); |
| 1938 | |
| 1939 | size_t decrypted_length = 0; |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1940 | EncryptionLevel decrypted_level; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1941 | if (!DecryptPayload(encrypted, associated_data, *header, decrypted_buffer, |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1942 | buffer_length, &decrypted_length, &decrypted_level)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1943 | RecordDroppedPacketReason(DroppedPacketReason::DECRYPTION_FAILURE); |
| 1944 | set_detailed_error("Unable to decrypt payload."); |
| 1945 | return RaiseError(QUIC_DECRYPTION_FAILURE); |
| 1946 | } |
| 1947 | |
| 1948 | QuicDataReader reader(decrypted_buffer, decrypted_length); |
| 1949 | |
| 1950 | // Update the largest packet number after we have decrypted the packet |
| 1951 | // so we are confident is not attacker controlled. |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 1952 | if (supports_multiple_packet_number_spaces_) { |
| 1953 | largest_decrypted_packet_numbers_[QuicUtils::GetPacketNumberSpace( |
| 1954 | decrypted_level)] |
| 1955 | .UpdateMax(header->packet_number); |
| 1956 | } else { |
| 1957 | largest_packet_number_.UpdateMax(header->packet_number); |
| 1958 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1959 | |
| 1960 | if (!visitor_->OnPacketHeader(*header)) { |
| 1961 | // The visitor suppresses further processing of the packet. |
| 1962 | return true; |
| 1963 | } |
| 1964 | |
dschinazi | e8d7fa7 | 2019-04-05 14:44:40 -0700 | [diff] [blame] | 1965 | if (packet.length() > kMaxIncomingPacketSize) { |
| 1966 | set_detailed_error("Packet too large."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1967 | return RaiseError(QUIC_PACKET_TOO_LARGE); |
| 1968 | } |
| 1969 | |
| 1970 | // Handle the payload. |
| 1971 | if (!ProcessFrameData(&reader, *header)) { |
| 1972 | DCHECK_NE(QUIC_NO_ERROR, error_); // ProcessFrameData sets the error. |
| 1973 | DCHECK_NE("", detailed_error_); |
| 1974 | QUIC_DLOG(WARNING) << ENDPOINT << "Unable to process frame data. Error: " |
| 1975 | << detailed_error_; |
| 1976 | return false; |
| 1977 | } |
| 1978 | |
| 1979 | visitor_->OnPacketComplete(); |
| 1980 | return true; |
| 1981 | } |
| 1982 | |
| 1983 | bool QuicFramer::ProcessPublicResetPacket(QuicDataReader* reader, |
| 1984 | const QuicPacketHeader& header) { |
| 1985 | QuicPublicResetPacket packet(header.destination_connection_id); |
| 1986 | |
| 1987 | std::unique_ptr<CryptoHandshakeMessage> reset( |
| 1988 | CryptoFramer::ParseMessage(reader->ReadRemainingPayload())); |
| 1989 | if (!reset.get()) { |
| 1990 | set_detailed_error("Unable to read reset message."); |
| 1991 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PUBLIC_RESET_PACKET); |
| 1992 | return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 1993 | } |
| 1994 | if (reset->tag() != kPRST) { |
| 1995 | set_detailed_error("Incorrect message tag."); |
| 1996 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PUBLIC_RESET_PACKET); |
| 1997 | return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 1998 | } |
| 1999 | |
| 2000 | if (reset->GetUint64(kRNON, &packet.nonce_proof) != QUIC_NO_ERROR) { |
| 2001 | set_detailed_error("Unable to read nonce proof."); |
| 2002 | RecordDroppedPacketReason(DroppedPacketReason::INVALID_PUBLIC_RESET_PACKET); |
| 2003 | return RaiseError(QUIC_INVALID_PUBLIC_RST_PACKET); |
| 2004 | } |
| 2005 | // TODO(satyamshekhar): validate nonce to protect against DoS. |
| 2006 | |
| 2007 | QuicStringPiece address; |
| 2008 | if (reset->GetStringPiece(kCADR, &address)) { |
| 2009 | QuicSocketAddressCoder address_coder; |
| 2010 | if (address_coder.Decode(address.data(), address.length())) { |
| 2011 | packet.client_address = |
| 2012 | QuicSocketAddress(address_coder.ip(), address_coder.port()); |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | QuicStringPiece endpoint_id; |
| 2017 | if (perspective_ == Perspective::IS_CLIENT && |
| 2018 | reset->GetStringPiece(kEPID, &endpoint_id)) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 2019 | packet.endpoint_id = std::string(endpoint_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2020 | packet.endpoint_id += '\0'; |
| 2021 | } |
| 2022 | |
| 2023 | visitor_->OnPublicResetPacket(packet); |
| 2024 | return true; |
| 2025 | } |
| 2026 | |
| 2027 | bool QuicFramer::IsIetfStatelessResetPacket( |
| 2028 | const QuicPacketHeader& header) const { |
| 2029 | QUIC_BUG_IF(header.has_possible_stateless_reset_token && |
| 2030 | perspective_ != Perspective::IS_CLIENT) |
| 2031 | << "has_possible_stateless_reset_token can only be true at client side."; |
| 2032 | return header.form == IETF_QUIC_SHORT_HEADER_PACKET && |
| 2033 | header.has_possible_stateless_reset_token && |
| 2034 | visitor_->IsValidStatelessResetToken( |
| 2035 | header.possible_stateless_reset_token); |
| 2036 | } |
| 2037 | |
| 2038 | bool QuicFramer::HasEncrypterOfEncryptionLevel(EncryptionLevel level) const { |
| 2039 | return encrypter_[level] != nullptr; |
| 2040 | } |
| 2041 | |
| 2042 | bool QuicFramer::AppendPacketHeader(const QuicPacketHeader& header, |
| 2043 | QuicDataWriter* writer, |
| 2044 | size_t* length_field_offset) { |
| 2045 | if (transport_version() > QUIC_VERSION_43) { |
| 2046 | return AppendIetfPacketHeader(header, writer, length_field_offset); |
| 2047 | } |
| 2048 | QUIC_DVLOG(1) << ENDPOINT << "Appending header: " << header; |
| 2049 | uint8_t public_flags = 0; |
| 2050 | if (header.reset_flag) { |
| 2051 | public_flags |= PACKET_PUBLIC_FLAGS_RST; |
| 2052 | } |
| 2053 | if (header.version_flag) { |
| 2054 | public_flags |= PACKET_PUBLIC_FLAGS_VERSION; |
| 2055 | } |
| 2056 | |
| 2057 | public_flags |= GetPacketNumberFlags(header.packet_number_length) |
| 2058 | << kPublicHeaderSequenceNumberShift; |
| 2059 | |
| 2060 | if (header.nonce != nullptr) { |
| 2061 | DCHECK_EQ(Perspective::IS_SERVER, perspective_); |
| 2062 | public_flags |= PACKET_PUBLIC_FLAGS_NONCE; |
| 2063 | } |
| 2064 | DCHECK_EQ(CONNECTION_ID_ABSENT, header.source_connection_id_included); |
| 2065 | switch (header.destination_connection_id_included) { |
| 2066 | case CONNECTION_ID_ABSENT: |
| 2067 | if (!writer->WriteUInt8(public_flags | |
| 2068 | PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID)) { |
| 2069 | return false; |
| 2070 | } |
| 2071 | break; |
| 2072 | case CONNECTION_ID_PRESENT: |
| 2073 | QUIC_BUG_IF(!QuicUtils::IsConnectionIdValidForVersion( |
| 2074 | header.destination_connection_id, transport_version())) |
| 2075 | << "AppendPacketHeader: attempted to use connection ID " |
| 2076 | << header.destination_connection_id |
| 2077 | << " which is invalid with version " |
| 2078 | << QuicVersionToString(transport_version()); |
| 2079 | |
| 2080 | public_flags |= PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID; |
| 2081 | if (perspective_ == Perspective::IS_CLIENT) { |
| 2082 | public_flags |= PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID_OLD; |
| 2083 | } |
| 2084 | if (!writer->WriteUInt8(public_flags) || |
| 2085 | !writer->WriteConnectionId(header.destination_connection_id)) { |
| 2086 | return false; |
| 2087 | } |
| 2088 | break; |
| 2089 | } |
| 2090 | last_serialized_connection_id_ = header.destination_connection_id; |
| 2091 | |
| 2092 | if (header.version_flag) { |
| 2093 | DCHECK_EQ(Perspective::IS_CLIENT, perspective_); |
| 2094 | QuicVersionLabel version_label = CreateQuicVersionLabel(version_); |
| 2095 | // TODO(rch): Use WriteUInt32() once QUIC_VERSION_35 is removed. |
| 2096 | if (!writer->WriteTag(QuicEndian::NetToHost32(version_label))) { |
| 2097 | return false; |
| 2098 | } |
| 2099 | |
| 2100 | QUIC_DVLOG(1) << ENDPOINT << "label = '" |
| 2101 | << QuicVersionLabelToString(version_label) << "'"; |
| 2102 | } |
| 2103 | |
| 2104 | if (header.nonce != nullptr && |
| 2105 | !writer->WriteBytes(header.nonce, kDiversificationNonceSize)) { |
| 2106 | return false; |
| 2107 | } |
| 2108 | |
| 2109 | if (!AppendPacketNumber(header.packet_number_length, header.packet_number, |
| 2110 | writer)) { |
| 2111 | return false; |
| 2112 | } |
| 2113 | |
| 2114 | return true; |
| 2115 | } |
| 2116 | |
| 2117 | bool QuicFramer::AppendIetfHeaderTypeByte(const QuicPacketHeader& header, |
| 2118 | QuicDataWriter* writer) { |
| 2119 | uint8_t type = 0; |
| 2120 | if (transport_version() > QUIC_VERSION_44) { |
| 2121 | if (header.version_flag) { |
| 2122 | type = static_cast<uint8_t>( |
| 2123 | FLAGS_LONG_HEADER | FLAGS_FIXED_BIT | |
| 2124 | LongHeaderTypeToOnWireValue(transport_version(), |
| 2125 | header.long_packet_type) | |
| 2126 | PacketNumberLengthToOnWireValue(transport_version(), |
| 2127 | header.packet_number_length)); |
| 2128 | } else { |
| 2129 | type = static_cast<uint8_t>( |
| 2130 | FLAGS_FIXED_BIT | |
| 2131 | PacketNumberLengthToOnWireValue(transport_version(), |
| 2132 | header.packet_number_length)); |
| 2133 | } |
| 2134 | return writer->WriteUInt8(type); |
| 2135 | } |
| 2136 | |
| 2137 | if (header.version_flag) { |
| 2138 | type = static_cast<uint8_t>( |
| 2139 | FLAGS_LONG_HEADER | LongHeaderTypeToOnWireValue( |
| 2140 | transport_version(), header.long_packet_type)); |
| 2141 | DCHECK_EQ(PACKET_4BYTE_PACKET_NUMBER, header.packet_number_length); |
| 2142 | } else { |
| 2143 | type |= FLAGS_SHORT_HEADER_RESERVED_1; |
| 2144 | type |= FLAGS_SHORT_HEADER_RESERVED_2; |
| 2145 | DCHECK_GE(PACKET_4BYTE_PACKET_NUMBER, header.packet_number_length); |
| 2146 | type |= PacketNumberLengthToOnWireValue(transport_version(), |
| 2147 | header.packet_number_length); |
| 2148 | } |
| 2149 | return writer->WriteUInt8(type); |
| 2150 | } |
| 2151 | |
| 2152 | bool QuicFramer::AppendIetfPacketHeader(const QuicPacketHeader& header, |
| 2153 | QuicDataWriter* writer, |
| 2154 | size_t* length_field_offset) { |
| 2155 | QUIC_DVLOG(1) << ENDPOINT << "Appending IETF header: " << header; |
| 2156 | QUIC_BUG_IF(!QuicUtils::IsConnectionIdValidForVersion( |
| 2157 | header.destination_connection_id, transport_version())) |
| 2158 | << "AppendIetfPacketHeader: attempted to use connection ID " |
| 2159 | << header.destination_connection_id << " which is invalid with version " |
| 2160 | << QuicVersionToString(transport_version()); |
| 2161 | if (!AppendIetfHeaderTypeByte(header, writer)) { |
| 2162 | return false; |
| 2163 | } |
| 2164 | |
| 2165 | if (header.version_flag) { |
| 2166 | // Append version for long header. |
| 2167 | QuicVersionLabel version_label = CreateQuicVersionLabel(version_); |
| 2168 | // TODO(rch): Use WriteUInt32() once QUIC_VERSION_35 is removed. |
| 2169 | if (!writer->WriteTag(QuicEndian::NetToHost32(version_label))) { |
| 2170 | return false; |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | // Append connection ID. |
| 2175 | if (!QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
| 2176 | transport_version()) && |
| 2177 | !GetQuicReloadableFlag(quic_use_new_append_connection_id)) { |
| 2178 | if (!AppendIetfConnectionId( |
| 2179 | header.version_flag, header.destination_connection_id, |
| 2180 | GetIncludedDestinationConnectionIdLength(header), |
| 2181 | header.source_connection_id, |
| 2182 | GetIncludedSourceConnectionIdLength(header), writer)) { |
| 2183 | return false; |
| 2184 | } |
| 2185 | } else { |
| 2186 | QUIC_RELOADABLE_FLAG_COUNT_N(quic_use_new_append_connection_id, 2, 2); |
| 2187 | if (!AppendIetfConnectionIdsNew( |
| 2188 | header.version_flag, |
| 2189 | header.destination_connection_id_included != CONNECTION_ID_ABSENT |
| 2190 | ? header.destination_connection_id |
| 2191 | : EmptyQuicConnectionId(), |
| 2192 | header.source_connection_id_included != CONNECTION_ID_ABSENT |
| 2193 | ? header.source_connection_id |
| 2194 | : EmptyQuicConnectionId(), |
| 2195 | writer)) { |
| 2196 | return false; |
| 2197 | } |
| 2198 | } |
| 2199 | last_serialized_connection_id_ = header.destination_connection_id; |
| 2200 | |
| 2201 | if (QuicVersionHasLongHeaderLengths(transport_version()) && |
| 2202 | header.version_flag) { |
| 2203 | if (header.long_packet_type == INITIAL) { |
| 2204 | // Write retry token length. |
| 2205 | if (!writer->WriteVarInt62(header.retry_token.length(), |
| 2206 | header.retry_token_length_length)) { |
| 2207 | return false; |
| 2208 | } |
| 2209 | // Write retry token. |
| 2210 | if (!header.retry_token.empty() && |
| 2211 | !writer->WriteStringPiece(header.retry_token)) { |
| 2212 | return false; |
| 2213 | } |
| 2214 | } |
| 2215 | if (length_field_offset != nullptr) { |
| 2216 | *length_field_offset = writer->length(); |
| 2217 | } |
| 2218 | // Add fake length to reserve two bytes to add length in later. |
| 2219 | writer->WriteVarInt62(256); |
| 2220 | } else if (length_field_offset != nullptr) { |
| 2221 | *length_field_offset = 0; |
| 2222 | } |
| 2223 | |
| 2224 | // Append packet number. |
| 2225 | if (!AppendPacketNumber(header.packet_number_length, header.packet_number, |
| 2226 | writer)) { |
| 2227 | return false; |
| 2228 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 2229 | last_written_packet_number_length_ = header.packet_number_length; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2230 | |
| 2231 | if (!header.version_flag) { |
| 2232 | return true; |
| 2233 | } |
| 2234 | |
| 2235 | if (header.nonce != nullptr) { |
| 2236 | DCHECK(header.version_flag); |
| 2237 | DCHECK_EQ(ZERO_RTT_PROTECTED, header.long_packet_type); |
| 2238 | DCHECK_EQ(Perspective::IS_SERVER, perspective_); |
| 2239 | if (!writer->WriteBytes(header.nonce, kDiversificationNonceSize)) { |
| 2240 | return false; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | return true; |
| 2245 | } |
| 2246 | |
| 2247 | const QuicTime::Delta QuicFramer::CalculateTimestampFromWire( |
| 2248 | uint32_t time_delta_us) { |
| 2249 | // The new time_delta might have wrapped to the next epoch, or it |
| 2250 | // might have reverse wrapped to the previous epoch, or it might |
| 2251 | // remain in the same epoch. Select the time closest to the previous |
| 2252 | // time. |
| 2253 | // |
| 2254 | // epoch_delta is the delta between epochs. A delta is 4 bytes of |
| 2255 | // microseconds. |
| 2256 | const uint64_t epoch_delta = UINT64_C(1) << 32; |
| 2257 | uint64_t epoch = last_timestamp_.ToMicroseconds() & ~(epoch_delta - 1); |
| 2258 | // Wrapping is safe here because a wrapped value will not be ClosestTo below. |
| 2259 | uint64_t prev_epoch = epoch - epoch_delta; |
| 2260 | uint64_t next_epoch = epoch + epoch_delta; |
| 2261 | |
| 2262 | uint64_t time = ClosestTo( |
| 2263 | last_timestamp_.ToMicroseconds(), epoch + time_delta_us, |
| 2264 | ClosestTo(last_timestamp_.ToMicroseconds(), prev_epoch + time_delta_us, |
| 2265 | next_epoch + time_delta_us)); |
| 2266 | |
| 2267 | return QuicTime::Delta::FromMicroseconds(time); |
| 2268 | } |
| 2269 | |
| 2270 | uint64_t QuicFramer::CalculatePacketNumberFromWire( |
| 2271 | QuicPacketNumberLength packet_number_length, |
| 2272 | QuicPacketNumber base_packet_number, |
| 2273 | uint64_t packet_number) const { |
| 2274 | // The new packet number might have wrapped to the next epoch, or |
| 2275 | // it might have reverse wrapped to the previous epoch, or it might |
| 2276 | // remain in the same epoch. Select the packet number closest to the |
| 2277 | // next expected packet number, the previous packet number plus 1. |
| 2278 | |
| 2279 | // epoch_delta is the delta between epochs the packet number was serialized |
| 2280 | // with, so the correct value is likely the same epoch as the last sequence |
| 2281 | // number or an adjacent epoch. |
| 2282 | if (!base_packet_number.IsInitialized()) { |
| 2283 | return packet_number; |
| 2284 | } |
| 2285 | const uint64_t epoch_delta = UINT64_C(1) << (8 * packet_number_length); |
| 2286 | uint64_t next_packet_number = base_packet_number.ToUint64() + 1; |
| 2287 | uint64_t epoch = base_packet_number.ToUint64() & ~(epoch_delta - 1); |
| 2288 | uint64_t prev_epoch = epoch - epoch_delta; |
| 2289 | uint64_t next_epoch = epoch + epoch_delta; |
| 2290 | |
| 2291 | return ClosestTo(next_packet_number, epoch + packet_number, |
| 2292 | ClosestTo(next_packet_number, prev_epoch + packet_number, |
| 2293 | next_epoch + packet_number)); |
| 2294 | } |
| 2295 | |
| 2296 | bool QuicFramer::ProcessPublicHeader(QuicDataReader* reader, |
| 2297 | bool packet_has_ietf_packet_header, |
| 2298 | QuicPacketHeader* header) { |
| 2299 | if (packet_has_ietf_packet_header) { |
| 2300 | return ProcessIetfPacketHeader(reader, header); |
| 2301 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2302 | uint8_t public_flags; |
| 2303 | if (!reader->ReadBytes(&public_flags, 1)) { |
| 2304 | set_detailed_error("Unable to read public flags."); |
| 2305 | return false; |
| 2306 | } |
| 2307 | |
| 2308 | header->reset_flag = (public_flags & PACKET_PUBLIC_FLAGS_RST) != 0; |
| 2309 | header->version_flag = (public_flags & PACKET_PUBLIC_FLAGS_VERSION) != 0; |
| 2310 | |
| 2311 | if (validate_flags_ && !header->version_flag && |
| 2312 | public_flags > PACKET_PUBLIC_FLAGS_MAX) { |
| 2313 | set_detailed_error("Illegal public flags value."); |
| 2314 | return false; |
| 2315 | } |
| 2316 | |
| 2317 | if (header->reset_flag && header->version_flag) { |
| 2318 | set_detailed_error("Got version flag in reset packet"); |
| 2319 | return false; |
| 2320 | } |
| 2321 | |
| 2322 | switch (public_flags & PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID) { |
| 2323 | case PACKET_PUBLIC_FLAGS_8BYTE_CONNECTION_ID: |
| 2324 | if (!reader->ReadConnectionId(&header->destination_connection_id, |
| 2325 | kQuicDefaultConnectionIdLength)) { |
| 2326 | set_detailed_error("Unable to read ConnectionId."); |
| 2327 | return false; |
| 2328 | } |
| 2329 | header->destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 2330 | break; |
| 2331 | case PACKET_PUBLIC_FLAGS_0BYTE_CONNECTION_ID: |
| 2332 | header->destination_connection_id_included = CONNECTION_ID_ABSENT; |
| 2333 | header->destination_connection_id = last_serialized_connection_id_; |
| 2334 | break; |
| 2335 | } |
| 2336 | |
| 2337 | header->packet_number_length = ReadSequenceNumberLength( |
| 2338 | public_flags >> kPublicHeaderSequenceNumberShift); |
| 2339 | |
| 2340 | // Read the version only if the packet is from the client. |
| 2341 | // version flag from the server means version negotiation packet. |
| 2342 | if (header->version_flag && perspective_ == Perspective::IS_SERVER) { |
| 2343 | QuicVersionLabel version_label; |
fayang | 4031554 | 2019-05-09 09:19:09 -0700 | [diff] [blame^] | 2344 | if (!ProcessVersionLabel(reader, &version_label)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2345 | set_detailed_error("Unable to read protocol version."); |
| 2346 | return false; |
| 2347 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2348 | // If the version from the new packet is the same as the version of this |
| 2349 | // framer, then the public flags should be set to something we understand. |
| 2350 | // If not, this raises an error. |
| 2351 | last_version_label_ = version_label; |
| 2352 | ParsedQuicVersion version = ParseQuicVersionLabel(version_label); |
| 2353 | if (version == version_ && public_flags > PACKET_PUBLIC_FLAGS_MAX) { |
| 2354 | set_detailed_error("Illegal public flags value."); |
| 2355 | return false; |
| 2356 | } |
| 2357 | header->version = version; |
| 2358 | } |
| 2359 | |
| 2360 | // A nonce should only be present in packets from the server to the client, |
| 2361 | // which are neither version negotiation nor public reset packets. |
| 2362 | if (public_flags & PACKET_PUBLIC_FLAGS_NONCE && |
| 2363 | !(public_flags & PACKET_PUBLIC_FLAGS_VERSION) && |
| 2364 | !(public_flags & PACKET_PUBLIC_FLAGS_RST) && |
| 2365 | // The nonce flag from a client is ignored and is assumed to be an older |
| 2366 | // client indicating an eight-byte connection ID. |
| 2367 | perspective_ == Perspective::IS_CLIENT) { |
| 2368 | if (!reader->ReadBytes(reinterpret_cast<uint8_t*>(last_nonce_.data()), |
| 2369 | last_nonce_.size())) { |
| 2370 | set_detailed_error("Unable to read nonce."); |
| 2371 | return false; |
| 2372 | } |
| 2373 | header->nonce = &last_nonce_; |
| 2374 | } else { |
| 2375 | header->nonce = nullptr; |
| 2376 | } |
| 2377 | |
| 2378 | return true; |
| 2379 | } |
| 2380 | |
| 2381 | // static |
| 2382 | QuicPacketNumberLength QuicFramer::GetMinPacketNumberLength( |
| 2383 | QuicTransportVersion version, |
| 2384 | QuicPacketNumber packet_number) { |
| 2385 | DCHECK(packet_number.IsInitialized()); |
| 2386 | if (packet_number < QuicPacketNumber(1 << (PACKET_1BYTE_PACKET_NUMBER * 8))) { |
| 2387 | return PACKET_1BYTE_PACKET_NUMBER; |
| 2388 | } else if (packet_number < |
| 2389 | QuicPacketNumber(1 << (PACKET_2BYTE_PACKET_NUMBER * 8))) { |
| 2390 | return PACKET_2BYTE_PACKET_NUMBER; |
| 2391 | } else if (packet_number < |
| 2392 | QuicPacketNumber(UINT64_C(1) |
| 2393 | << (PACKET_4BYTE_PACKET_NUMBER * 8))) { |
| 2394 | return PACKET_4BYTE_PACKET_NUMBER; |
| 2395 | } else { |
| 2396 | return PACKET_6BYTE_PACKET_NUMBER; |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | // static |
| 2401 | uint8_t QuicFramer::GetPacketNumberFlags( |
| 2402 | QuicPacketNumberLength packet_number_length) { |
| 2403 | switch (packet_number_length) { |
| 2404 | case PACKET_1BYTE_PACKET_NUMBER: |
| 2405 | return PACKET_FLAGS_1BYTE_PACKET; |
| 2406 | case PACKET_2BYTE_PACKET_NUMBER: |
| 2407 | return PACKET_FLAGS_2BYTE_PACKET; |
| 2408 | case PACKET_4BYTE_PACKET_NUMBER: |
| 2409 | return PACKET_FLAGS_4BYTE_PACKET; |
| 2410 | case PACKET_6BYTE_PACKET_NUMBER: |
| 2411 | case PACKET_8BYTE_PACKET_NUMBER: |
| 2412 | return PACKET_FLAGS_8BYTE_PACKET; |
| 2413 | default: |
| 2414 | QUIC_BUG << "Unreachable case statement."; |
| 2415 | return PACKET_FLAGS_8BYTE_PACKET; |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | // static |
| 2420 | QuicFramer::AckFrameInfo QuicFramer::GetAckFrameInfo( |
| 2421 | const QuicAckFrame& frame) { |
| 2422 | AckFrameInfo new_ack_info; |
| 2423 | if (frame.packets.Empty()) { |
| 2424 | return new_ack_info; |
| 2425 | } |
| 2426 | // The first block is the last interval. It isn't encoded with the gap-length |
| 2427 | // encoding, so skip it. |
| 2428 | new_ack_info.first_block_length = frame.packets.LastIntervalLength(); |
| 2429 | auto itr = frame.packets.rbegin(); |
| 2430 | QuicPacketNumber previous_start = itr->min(); |
| 2431 | new_ack_info.max_block_length = PacketNumberIntervalLength(*itr); |
| 2432 | ++itr; |
| 2433 | |
| 2434 | // Don't do any more work after getting information for 256 ACK blocks; any |
| 2435 | // more can't be encoded anyway. |
| 2436 | for (; itr != frame.packets.rend() && |
| 2437 | new_ack_info.num_ack_blocks < std::numeric_limits<uint8_t>::max(); |
| 2438 | previous_start = itr->min(), ++itr) { |
| 2439 | const auto& interval = *itr; |
| 2440 | const QuicPacketCount total_gap = previous_start - interval.max(); |
| 2441 | new_ack_info.num_ack_blocks += |
| 2442 | (total_gap + std::numeric_limits<uint8_t>::max() - 1) / |
| 2443 | std::numeric_limits<uint8_t>::max(); |
| 2444 | new_ack_info.max_block_length = std::max( |
| 2445 | new_ack_info.max_block_length, PacketNumberIntervalLength(interval)); |
| 2446 | } |
| 2447 | return new_ack_info; |
| 2448 | } |
| 2449 | |
| 2450 | bool QuicFramer::ProcessUnauthenticatedHeader(QuicDataReader* encrypted_reader, |
| 2451 | QuicPacketHeader* header) { |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 2452 | QuicPacketNumber base_packet_number; |
| 2453 | if (supports_multiple_packet_number_spaces_) { |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 2454 | PacketNumberSpace pn_space = GetPacketNumberSpace(*header); |
| 2455 | if (pn_space == NUM_PACKET_NUMBER_SPACES) { |
| 2456 | set_detailed_error("Unable to determine packet number space."); |
| 2457 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 2458 | } |
| 2459 | base_packet_number = largest_decrypted_packet_numbers_[pn_space]; |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 2460 | } else { |
| 2461 | base_packet_number = largest_packet_number_; |
| 2462 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2463 | uint64_t full_packet_number; |
| 2464 | if (!ProcessAndCalculatePacketNumber( |
| 2465 | encrypted_reader, header->packet_number_length, base_packet_number, |
| 2466 | &full_packet_number)) { |
| 2467 | set_detailed_error("Unable to read packet number."); |
| 2468 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 2469 | } |
| 2470 | |
| 2471 | if (!IsValidFullPacketNumber(full_packet_number, transport_version())) { |
| 2472 | set_detailed_error("packet numbers cannot be 0."); |
| 2473 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 2474 | } |
| 2475 | header->packet_number = QuicPacketNumber(full_packet_number); |
| 2476 | |
| 2477 | if (!visitor_->OnUnauthenticatedHeader(*header)) { |
| 2478 | set_detailed_error( |
| 2479 | "Visitor asked to stop processing of unauthenticated header."); |
| 2480 | return false; |
| 2481 | } |
nharper | 3f28356 | 2019-05-02 16:37:12 -0700 | [diff] [blame] | 2482 | // The function we are in is called because the framer believes that it is |
| 2483 | // processing a packet that uses the non-IETF (i.e. Google QUIC) packet header |
| 2484 | // type. Usually, the framer makes that decision based on the framer's |
| 2485 | // version, but when the framer is used with Perspective::IS_SERVER, then |
| 2486 | // before version negotiation is complete (specifically, before |
| 2487 | // InferPacketHeaderTypeFromVersion is called), this decision is made based on |
| 2488 | // the type byte of the packet. |
| 2489 | // |
| 2490 | // If the framer's version KnowsWhichDecrypterToUse, then that version expects |
| 2491 | // to use the IETF packet header type. If that's the case and we're in this |
| 2492 | // function, then the packet received is invalid: the framer was expecting an |
| 2493 | // IETF packet header and didn't get one. |
| 2494 | if (version().KnowsWhichDecrypterToUse()) { |
nharper | a745e39 | 2019-04-19 12:05:15 -0700 | [diff] [blame] | 2495 | set_detailed_error("Invalid public header type for expected version."); |
| 2496 | return RaiseError(QUIC_INVALID_PACKET_HEADER); |
| 2497 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2498 | return true; |
| 2499 | } |
| 2500 | |
| 2501 | bool QuicFramer::ProcessIetfHeaderTypeByte(QuicDataReader* reader, |
| 2502 | QuicPacketHeader* header) { |
| 2503 | uint8_t type; |
| 2504 | if (!reader->ReadBytes(&type, 1)) { |
| 2505 | set_detailed_error("Unable to read type."); |
| 2506 | return false; |
| 2507 | } |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 2508 | header->type_byte = type; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2509 | // Determine whether this is a long or short header. |
| 2510 | header->form = type & FLAGS_LONG_HEADER ? IETF_QUIC_LONG_HEADER_PACKET |
| 2511 | : IETF_QUIC_SHORT_HEADER_PACKET; |
| 2512 | if (header->form == IETF_QUIC_LONG_HEADER_PACKET) { |
| 2513 | // Version is always present in long headers. |
| 2514 | header->version_flag = true; |
| 2515 | // Long header packets received by client must include 8-byte source |
| 2516 | // connection ID, and those received by server must include 8-byte |
| 2517 | // destination connection ID. |
| 2518 | header->destination_connection_id_included = |
| 2519 | perspective_ == Perspective::IS_CLIENT ? CONNECTION_ID_ABSENT |
| 2520 | : CONNECTION_ID_PRESENT; |
| 2521 | header->source_connection_id_included = |
| 2522 | perspective_ == Perspective::IS_CLIENT ? CONNECTION_ID_PRESENT |
| 2523 | : CONNECTION_ID_ABSENT; |
| 2524 | // Read version tag. |
| 2525 | QuicVersionLabel version_label; |
fayang | 4031554 | 2019-05-09 09:19:09 -0700 | [diff] [blame^] | 2526 | if (!ProcessVersionLabel(reader, &version_label)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2527 | set_detailed_error("Unable to read protocol version."); |
| 2528 | return false; |
| 2529 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2530 | if (!version_label) { |
| 2531 | // Version label is 0 indicating this is a version negotiation packet. |
| 2532 | header->long_packet_type = VERSION_NEGOTIATION; |
| 2533 | } else { |
| 2534 | header->version = ParseQuicVersionLabel(version_label); |
| 2535 | if (header->version.transport_version != QUIC_VERSION_UNSUPPORTED) { |
| 2536 | if (header->version.transport_version > QUIC_VERSION_44 && |
| 2537 | !(type & FLAGS_FIXED_BIT)) { |
| 2538 | set_detailed_error("Fixed bit is 0 in long header."); |
| 2539 | return false; |
| 2540 | } |
| 2541 | if (!GetLongHeaderType(header->version.transport_version, type, |
| 2542 | &header->long_packet_type)) { |
| 2543 | set_detailed_error("Illegal long header type value."); |
| 2544 | return false; |
| 2545 | } |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 2546 | if (header->long_packet_type == RETRY) { |
| 2547 | if (!version().SupportsRetry()) { |
| 2548 | set_detailed_error("RETRY not supported in this version."); |
| 2549 | return false; |
| 2550 | } |
| 2551 | if (perspective_ == Perspective::IS_SERVER) { |
| 2552 | set_detailed_error("Client-initiated RETRY is invalid."); |
| 2553 | return false; |
| 2554 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 2555 | } else if (!header->version.HasHeaderProtection()) { |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 2556 | header->packet_number_length = GetLongHeaderPacketNumberLength( |
| 2557 | header->version.transport_version, type); |
nharper | 2ceb97c | 2019-04-19 11:38:59 -0700 | [diff] [blame] | 2558 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | if (header->long_packet_type != VERSION_NEGOTIATION) { |
| 2562 | // Do not save version of version negotiation packet. |
| 2563 | last_version_label_ = version_label; |
| 2564 | } |
| 2565 | |
| 2566 | QUIC_DVLOG(1) << ENDPOINT << "Received IETF long header: " |
| 2567 | << QuicUtils::QuicLongHeaderTypetoString( |
| 2568 | header->long_packet_type); |
| 2569 | return true; |
| 2570 | } |
| 2571 | |
| 2572 | QUIC_DVLOG(1) << ENDPOINT << "Received IETF short header"; |
| 2573 | // Version is not present in short headers. |
| 2574 | header->version_flag = false; |
| 2575 | // Connection ID length depends on the perspective. Client does not expect |
| 2576 | // destination connection ID, and server expects destination connection ID. |
| 2577 | header->destination_connection_id_included = |
| 2578 | perspective_ == Perspective::IS_CLIENT ? CONNECTION_ID_ABSENT |
| 2579 | : CONNECTION_ID_PRESENT; |
| 2580 | header->source_connection_id_included = CONNECTION_ID_ABSENT; |
| 2581 | if (infer_packet_header_type_from_version_ && |
| 2582 | transport_version() > QUIC_VERSION_44 && !(type & FLAGS_FIXED_BIT)) { |
| 2583 | set_detailed_error("Fixed bit is 0 in short header."); |
| 2584 | return false; |
| 2585 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 2586 | if (!header->version.HasHeaderProtection() && |
| 2587 | !GetShortHeaderPacketNumberLength(transport_version(), type, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2588 | infer_packet_header_type_from_version_, |
| 2589 | &header->packet_number_length)) { |
| 2590 | set_detailed_error("Illegal short header type value."); |
| 2591 | return false; |
| 2592 | } |
| 2593 | QUIC_DVLOG(1) << "packet_number_length = " << header->packet_number_length; |
| 2594 | return true; |
| 2595 | } |
| 2596 | |
fayang | 4031554 | 2019-05-09 09:19:09 -0700 | [diff] [blame^] | 2597 | // static |
| 2598 | bool QuicFramer::ProcessVersionLabel(QuicDataReader* reader, |
| 2599 | QuicVersionLabel* version_label) { |
| 2600 | if (!reader->ReadTag(version_label)) { |
| 2601 | return false; |
| 2602 | } |
| 2603 | // TODO(rch): Use ReadUInt32() once QUIC_VERSION_35 is removed. |
| 2604 | *version_label = QuicEndian::NetToHost32(*version_label); |
| 2605 | return true; |
| 2606 | } |
| 2607 | |
| 2608 | // static |
| 2609 | bool QuicFramer::ValidateIetfConnectionIdLength( |
| 2610 | uint8_t connection_id_lengths_byte, |
| 2611 | ParsedQuicVersion version, |
| 2612 | bool should_update_expected_connection_id_length, |
| 2613 | uint8_t* expected_connection_id_length, |
| 2614 | uint8_t* destination_connection_id_length, |
| 2615 | uint8_t* source_connection_id_length) { |
| 2616 | uint8_t dcil = |
| 2617 | (connection_id_lengths_byte & kDestinationConnectionIdLengthMask) >> 4; |
| 2618 | if (dcil != 0) { |
| 2619 | dcil += kConnectionIdLengthAdjustment; |
| 2620 | } |
| 2621 | if (should_update_expected_connection_id_length && |
| 2622 | *expected_connection_id_length != dcil) { |
| 2623 | QUIC_DVLOG(1) << "Updating expected_connection_id_length: " |
| 2624 | << static_cast<int>(*expected_connection_id_length) << " -> " |
| 2625 | << static_cast<int>(dcil); |
| 2626 | *expected_connection_id_length = dcil; |
| 2627 | } |
| 2628 | uint8_t scil = connection_id_lengths_byte & kSourceConnectionIdLengthMask; |
| 2629 | if (scil != 0) { |
| 2630 | scil += kConnectionIdLengthAdjustment; |
| 2631 | } |
| 2632 | if ((dcil != *destination_connection_id_length || |
| 2633 | scil != *source_connection_id_length) && |
| 2634 | !should_update_expected_connection_id_length && |
| 2635 | !QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
| 2636 | version.transport_version)) { |
| 2637 | // TODO(dschinazi): use the framer's version once the |
| 2638 | // OnProtocolVersionMismatch call is moved to before this is run. |
| 2639 | QUIC_DVLOG(1) << "dcil: " << static_cast<uint32_t>(dcil) |
| 2640 | << ", scil: " << static_cast<uint32_t>(scil); |
| 2641 | return false; |
| 2642 | } |
| 2643 | *destination_connection_id_length = dcil; |
| 2644 | *source_connection_id_length = scil; |
| 2645 | return true; |
| 2646 | } |
| 2647 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2648 | bool QuicFramer::ProcessIetfPacketHeader(QuicDataReader* reader, |
| 2649 | QuicPacketHeader* header) { |
| 2650 | if (!ProcessIetfHeaderTypeByte(reader, header)) { |
| 2651 | return false; |
| 2652 | } |
| 2653 | |
| 2654 | uint8_t destination_connection_id_length = |
| 2655 | header->destination_connection_id_included == CONNECTION_ID_PRESENT |
| 2656 | ? expected_connection_id_length_ |
| 2657 | : 0; |
| 2658 | uint8_t source_connection_id_length = |
| 2659 | header->source_connection_id_included == CONNECTION_ID_PRESENT |
| 2660 | ? expected_connection_id_length_ |
| 2661 | : 0; |
| 2662 | if (header->form == IETF_QUIC_LONG_HEADER_PACKET) { |
| 2663 | // Read and validate connection ID length. |
| 2664 | uint8_t connection_id_lengths_byte; |
| 2665 | if (!reader->ReadBytes(&connection_id_lengths_byte, 1)) { |
| 2666 | set_detailed_error("Unable to read ConnectionId length."); |
| 2667 | return false; |
| 2668 | } |
fayang | 4031554 | 2019-05-09 09:19:09 -0700 | [diff] [blame^] | 2669 | if (!ValidateIetfConnectionIdLength( |
| 2670 | connection_id_lengths_byte, header->version, |
| 2671 | should_update_expected_connection_id_length_, |
| 2672 | &expected_connection_id_length_, &destination_connection_id_length, |
| 2673 | &source_connection_id_length)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2674 | set_detailed_error("Invalid ConnectionId length."); |
| 2675 | return false; |
| 2676 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2677 | } |
| 2678 | |
QUICHE team | 0131a5b | 2019-03-20 15:23:27 -0700 | [diff] [blame] | 2679 | DCHECK_LE(destination_connection_id_length, kQuicMaxConnectionIdLength); |
| 2680 | DCHECK_LE(source_connection_id_length, kQuicMaxConnectionIdLength); |
| 2681 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2682 | // Read connection ID. |
| 2683 | if (!reader->ReadConnectionId(&header->destination_connection_id, |
| 2684 | destination_connection_id_length)) { |
| 2685 | set_detailed_error("Unable to read Destination ConnectionId."); |
| 2686 | return false; |
| 2687 | } |
| 2688 | |
| 2689 | if (!reader->ReadConnectionId(&header->source_connection_id, |
| 2690 | source_connection_id_length)) { |
| 2691 | set_detailed_error("Unable to read Source ConnectionId."); |
| 2692 | return false; |
| 2693 | } |
| 2694 | |
| 2695 | if (header->source_connection_id_included == CONNECTION_ID_PRESENT) { |
| 2696 | // Set destination connection ID to source connection ID. |
| 2697 | DCHECK_EQ(EmptyQuicConnectionId(), header->destination_connection_id); |
| 2698 | header->destination_connection_id = header->source_connection_id; |
| 2699 | } else if (header->destination_connection_id_included == |
| 2700 | CONNECTION_ID_ABSENT) { |
| 2701 | header->destination_connection_id = last_serialized_connection_id_; |
| 2702 | } |
| 2703 | |
| 2704 | return true; |
| 2705 | } |
| 2706 | |
| 2707 | bool QuicFramer::ProcessAndCalculatePacketNumber( |
| 2708 | QuicDataReader* reader, |
| 2709 | QuicPacketNumberLength packet_number_length, |
| 2710 | QuicPacketNumber base_packet_number, |
| 2711 | uint64_t* packet_number) { |
| 2712 | uint64_t wire_packet_number; |
| 2713 | if (!reader->ReadBytesToUInt64(packet_number_length, &wire_packet_number)) { |
| 2714 | return false; |
| 2715 | } |
| 2716 | |
| 2717 | // TODO(ianswett): Explore the usefulness of trying multiple packet numbers |
| 2718 | // in case the first guess is incorrect. |
| 2719 | *packet_number = CalculatePacketNumberFromWire( |
| 2720 | packet_number_length, base_packet_number, wire_packet_number); |
| 2721 | return true; |
| 2722 | } |
| 2723 | |
| 2724 | bool QuicFramer::ProcessFrameData(QuicDataReader* reader, |
| 2725 | const QuicPacketHeader& header) { |
| 2726 | DCHECK_NE(QUIC_VERSION_99, version_.transport_version) |
| 2727 | << "Version 99 negotiated, but not processing frames as version 99."; |
| 2728 | if (reader->IsDoneReading()) { |
| 2729 | set_detailed_error("Packet has no frames."); |
| 2730 | return RaiseError(QUIC_MISSING_PAYLOAD); |
| 2731 | } |
| 2732 | while (!reader->IsDoneReading()) { |
| 2733 | uint8_t frame_type; |
| 2734 | if (!reader->ReadBytes(&frame_type, 1)) { |
| 2735 | set_detailed_error("Unable to read frame type."); |
| 2736 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 2737 | } |
| 2738 | const uint8_t special_mask = transport_version() <= QUIC_VERSION_44 |
| 2739 | ? kQuicFrameTypeBrokenMask |
| 2740 | : kQuicFrameTypeSpecialMask; |
| 2741 | if (frame_type & special_mask) { |
| 2742 | // Stream Frame |
| 2743 | if (frame_type & kQuicFrameTypeStreamMask) { |
| 2744 | QuicStreamFrame frame; |
| 2745 | if (!ProcessStreamFrame(reader, frame_type, &frame)) { |
| 2746 | return RaiseError(QUIC_INVALID_STREAM_DATA); |
| 2747 | } |
| 2748 | if (!visitor_->OnStreamFrame(frame)) { |
| 2749 | QUIC_DVLOG(1) << ENDPOINT |
| 2750 | << "Visitor asked to stop further processing."; |
| 2751 | // Returning true since there was no parsing error. |
| 2752 | return true; |
| 2753 | } |
| 2754 | continue; |
| 2755 | } |
| 2756 | |
| 2757 | // Ack Frame |
| 2758 | if (frame_type & kQuicFrameTypeAckMask) { |
| 2759 | if (!ProcessAckFrame(reader, frame_type)) { |
| 2760 | return RaiseError(QUIC_INVALID_ACK_DATA); |
| 2761 | } |
| 2762 | continue; |
| 2763 | } |
| 2764 | |
| 2765 | // This was a special frame type that did not match any |
| 2766 | // of the known ones. Error. |
| 2767 | set_detailed_error("Illegal frame type."); |
| 2768 | QUIC_DLOG(WARNING) << ENDPOINT << "Illegal frame type: " |
| 2769 | << static_cast<int>(frame_type); |
| 2770 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 2771 | } |
| 2772 | |
| 2773 | switch (frame_type) { |
| 2774 | case PADDING_FRAME: { |
| 2775 | QuicPaddingFrame frame; |
| 2776 | ProcessPaddingFrame(reader, &frame); |
| 2777 | if (!visitor_->OnPaddingFrame(frame)) { |
| 2778 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 2779 | // Returning true since there was no parsing error. |
| 2780 | return true; |
| 2781 | } |
| 2782 | continue; |
| 2783 | } |
| 2784 | |
| 2785 | case RST_STREAM_FRAME: { |
| 2786 | QuicRstStreamFrame frame; |
| 2787 | if (!ProcessRstStreamFrame(reader, &frame)) { |
| 2788 | return RaiseError(QUIC_INVALID_RST_STREAM_DATA); |
| 2789 | } |
| 2790 | if (!visitor_->OnRstStreamFrame(frame)) { |
| 2791 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 2792 | // Returning true since there was no parsing error. |
| 2793 | return true; |
| 2794 | } |
| 2795 | continue; |
| 2796 | } |
| 2797 | |
| 2798 | case CONNECTION_CLOSE_FRAME: { |
| 2799 | QuicConnectionCloseFrame frame; |
| 2800 | if (!ProcessConnectionCloseFrame(reader, &frame)) { |
| 2801 | return RaiseError(QUIC_INVALID_CONNECTION_CLOSE_DATA); |
| 2802 | } |
| 2803 | |
| 2804 | if (!visitor_->OnConnectionCloseFrame(frame)) { |
| 2805 | QUIC_DVLOG(1) << ENDPOINT |
| 2806 | << "Visitor asked to stop further processing."; |
| 2807 | // Returning true since there was no parsing error. |
| 2808 | return true; |
| 2809 | } |
| 2810 | continue; |
| 2811 | } |
| 2812 | |
| 2813 | case GOAWAY_FRAME: { |
| 2814 | QuicGoAwayFrame goaway_frame; |
| 2815 | if (!ProcessGoAwayFrame(reader, &goaway_frame)) { |
| 2816 | return RaiseError(QUIC_INVALID_GOAWAY_DATA); |
| 2817 | } |
| 2818 | if (!visitor_->OnGoAwayFrame(goaway_frame)) { |
| 2819 | QUIC_DVLOG(1) << ENDPOINT |
| 2820 | << "Visitor asked to stop further processing."; |
| 2821 | // Returning true since there was no parsing error. |
| 2822 | return true; |
| 2823 | } |
| 2824 | continue; |
| 2825 | } |
| 2826 | |
| 2827 | case WINDOW_UPDATE_FRAME: { |
| 2828 | QuicWindowUpdateFrame window_update_frame; |
| 2829 | if (!ProcessWindowUpdateFrame(reader, &window_update_frame)) { |
| 2830 | return RaiseError(QUIC_INVALID_WINDOW_UPDATE_DATA); |
| 2831 | } |
| 2832 | if (!visitor_->OnWindowUpdateFrame(window_update_frame)) { |
| 2833 | QUIC_DVLOG(1) << ENDPOINT |
| 2834 | << "Visitor asked to stop further processing."; |
| 2835 | // Returning true since there was no parsing error. |
| 2836 | return true; |
| 2837 | } |
| 2838 | continue; |
| 2839 | } |
| 2840 | |
| 2841 | case BLOCKED_FRAME: { |
| 2842 | QuicBlockedFrame blocked_frame; |
| 2843 | if (!ProcessBlockedFrame(reader, &blocked_frame)) { |
| 2844 | return RaiseError(QUIC_INVALID_BLOCKED_DATA); |
| 2845 | } |
| 2846 | if (!visitor_->OnBlockedFrame(blocked_frame)) { |
| 2847 | QUIC_DVLOG(1) << ENDPOINT |
| 2848 | << "Visitor asked to stop further processing."; |
| 2849 | // Returning true since there was no parsing error. |
| 2850 | return true; |
| 2851 | } |
| 2852 | continue; |
| 2853 | } |
| 2854 | |
| 2855 | case STOP_WAITING_FRAME: { |
ianswett | 97b690b | 2019-05-02 15:12:43 -0700 | [diff] [blame] | 2856 | if (GetQuicReloadableFlag(quic_do_not_accept_stop_waiting) && |
| 2857 | version_.transport_version >= QUIC_VERSION_44) { |
| 2858 | QUIC_RELOADABLE_FLAG_COUNT(quic_do_not_accept_stop_waiting); |
| 2859 | set_detailed_error("STOP WAITING not supported in version 44+."); |
| 2860 | return RaiseError(QUIC_INVALID_STOP_WAITING_DATA); |
| 2861 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2862 | QuicStopWaitingFrame stop_waiting_frame; |
| 2863 | if (!ProcessStopWaitingFrame(reader, header, &stop_waiting_frame)) { |
| 2864 | return RaiseError(QUIC_INVALID_STOP_WAITING_DATA); |
| 2865 | } |
| 2866 | if (!visitor_->OnStopWaitingFrame(stop_waiting_frame)) { |
| 2867 | QUIC_DVLOG(1) << ENDPOINT |
| 2868 | << "Visitor asked to stop further processing."; |
| 2869 | // Returning true since there was no parsing error. |
| 2870 | return true; |
| 2871 | } |
| 2872 | continue; |
| 2873 | } |
| 2874 | case PING_FRAME: { |
| 2875 | // Ping has no payload. |
| 2876 | QuicPingFrame ping_frame; |
| 2877 | if (!visitor_->OnPingFrame(ping_frame)) { |
| 2878 | QUIC_DVLOG(1) << ENDPOINT |
| 2879 | << "Visitor asked to stop further processing."; |
| 2880 | // Returning true since there was no parsing error. |
| 2881 | return true; |
| 2882 | } |
| 2883 | continue; |
| 2884 | } |
| 2885 | case IETF_EXTENSION_MESSAGE_NO_LENGTH: |
| 2886 | QUIC_FALLTHROUGH_INTENDED; |
| 2887 | case IETF_EXTENSION_MESSAGE: { |
| 2888 | QuicMessageFrame message_frame; |
| 2889 | if (!ProcessMessageFrame(reader, |
| 2890 | frame_type == IETF_EXTENSION_MESSAGE_NO_LENGTH, |
| 2891 | &message_frame)) { |
| 2892 | return RaiseError(QUIC_INVALID_MESSAGE_DATA); |
| 2893 | } |
| 2894 | if (!visitor_->OnMessageFrame(message_frame)) { |
| 2895 | QUIC_DVLOG(1) << ENDPOINT |
| 2896 | << "Visitor asked to stop further processing."; |
| 2897 | // Returning true since there was no parsing error. |
| 2898 | return true; |
| 2899 | } |
| 2900 | break; |
| 2901 | } |
| 2902 | case CRYPTO_FRAME: { |
QUICHE team | ea74008 | 2019-03-11 17:58:43 -0700 | [diff] [blame] | 2903 | if (!QuicVersionUsesCryptoFrames(version_.transport_version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2904 | set_detailed_error("Illegal frame type."); |
| 2905 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 2906 | } |
| 2907 | QuicCryptoFrame frame; |
| 2908 | if (!ProcessCryptoFrame(reader, &frame)) { |
| 2909 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 2910 | } |
| 2911 | if (!visitor_->OnCryptoFrame(frame)) { |
| 2912 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 2913 | // Returning true since there was no parsing error. |
| 2914 | return true; |
| 2915 | } |
| 2916 | break; |
| 2917 | } |
| 2918 | |
| 2919 | default: |
| 2920 | set_detailed_error("Illegal frame type."); |
| 2921 | QUIC_DLOG(WARNING) << ENDPOINT << "Illegal frame type: " |
| 2922 | << static_cast<int>(frame_type); |
| 2923 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | return true; |
| 2928 | } |
| 2929 | |
| 2930 | bool QuicFramer::ProcessIetfFrameData(QuicDataReader* reader, |
| 2931 | const QuicPacketHeader& header) { |
| 2932 | DCHECK_EQ(QUIC_VERSION_99, version_.transport_version) |
| 2933 | << "Attempt to process frames as IETF frames but version is " |
| 2934 | << version_.transport_version << ", not 99."; |
| 2935 | if (reader->IsDoneReading()) { |
| 2936 | set_detailed_error("Packet has no frames."); |
| 2937 | return RaiseError(QUIC_MISSING_PAYLOAD); |
| 2938 | } |
| 2939 | while (!reader->IsDoneReading()) { |
| 2940 | uint64_t frame_type; |
| 2941 | // Will be the number of bytes into which frame_type was encoded. |
| 2942 | size_t encoded_bytes = reader->BytesRemaining(); |
| 2943 | if (!reader->ReadVarInt62(&frame_type)) { |
| 2944 | set_detailed_error("Unable to read frame type."); |
| 2945 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 2946 | } |
| 2947 | |
| 2948 | // Is now the number of bytes into which the frame type was encoded. |
| 2949 | encoded_bytes -= reader->BytesRemaining(); |
| 2950 | |
| 2951 | // Check that the frame type is minimally encoded. |
| 2952 | if (encoded_bytes != |
| 2953 | static_cast<size_t>(QuicDataWriter::GetVarInt62Len(frame_type))) { |
| 2954 | // The frame type was not minimally encoded. |
| 2955 | set_detailed_error("Frame type not minimally encoded."); |
| 2956 | return RaiseError(IETF_QUIC_PROTOCOL_VIOLATION); |
| 2957 | } |
| 2958 | |
| 2959 | if (IS_IETF_STREAM_FRAME(frame_type)) { |
| 2960 | QuicStreamFrame frame; |
| 2961 | if (!ProcessIetfStreamFrame(reader, frame_type, &frame)) { |
| 2962 | return RaiseError(QUIC_INVALID_STREAM_DATA); |
| 2963 | } |
| 2964 | if (!visitor_->OnStreamFrame(frame)) { |
| 2965 | QUIC_DVLOG(1) << ENDPOINT |
| 2966 | << "Visitor asked to stop further processing."; |
| 2967 | // Returning true since there was no parsing error. |
| 2968 | return true; |
| 2969 | } |
| 2970 | } else { |
| 2971 | switch (frame_type) { |
| 2972 | case IETF_PADDING: { |
| 2973 | QuicPaddingFrame frame; |
| 2974 | ProcessPaddingFrame(reader, &frame); |
| 2975 | if (!visitor_->OnPaddingFrame(frame)) { |
| 2976 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 2977 | // Returning true since there was no parsing error. |
| 2978 | return true; |
| 2979 | } |
| 2980 | break; |
| 2981 | } |
| 2982 | case IETF_RST_STREAM: { |
| 2983 | QuicRstStreamFrame frame; |
| 2984 | if (!ProcessIetfResetStreamFrame(reader, &frame)) { |
| 2985 | return RaiseError(QUIC_INVALID_RST_STREAM_DATA); |
| 2986 | } |
| 2987 | if (!visitor_->OnRstStreamFrame(frame)) { |
| 2988 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 2989 | // Returning true since there was no parsing error. |
| 2990 | return true; |
| 2991 | } |
| 2992 | break; |
| 2993 | } |
fkastenholz | 04bd4f3 | 2019-04-16 12:24:38 -0700 | [diff] [blame] | 2994 | case IETF_APPLICATION_CLOSE: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2995 | case IETF_CONNECTION_CLOSE: { |
| 2996 | QuicConnectionCloseFrame frame; |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 2997 | if (!ProcessIetfConnectionCloseFrame( |
fkastenholz | 04bd4f3 | 2019-04-16 12:24:38 -0700 | [diff] [blame] | 2998 | reader, |
| 2999 | (frame_type == IETF_CONNECTION_CLOSE) |
| 3000 | ? IETF_QUIC_TRANSPORT_CONNECTION_CLOSE |
| 3001 | : IETF_QUIC_APPLICATION_CONNECTION_CLOSE, |
| 3002 | &frame)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3003 | return RaiseError(QUIC_INVALID_CONNECTION_CLOSE_DATA); |
| 3004 | } |
| 3005 | if (!visitor_->OnConnectionCloseFrame(frame)) { |
| 3006 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3007 | // Returning true since there was no parsing error. |
| 3008 | return true; |
| 3009 | } |
| 3010 | break; |
| 3011 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3012 | case IETF_MAX_DATA: { |
| 3013 | QuicWindowUpdateFrame frame; |
| 3014 | if (!ProcessMaxDataFrame(reader, &frame)) { |
| 3015 | return RaiseError(QUIC_INVALID_MAX_DATA_FRAME_DATA); |
| 3016 | } |
| 3017 | // TODO(fkastenholz): Or should we create a new visitor function, |
| 3018 | // OnMaxDataFrame()? |
| 3019 | if (!visitor_->OnWindowUpdateFrame(frame)) { |
| 3020 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3021 | // Returning true since there was no parsing error. |
| 3022 | return true; |
| 3023 | } |
| 3024 | break; |
| 3025 | } |
| 3026 | case IETF_MAX_STREAM_DATA: { |
| 3027 | QuicWindowUpdateFrame frame; |
| 3028 | if (!ProcessMaxStreamDataFrame(reader, &frame)) { |
| 3029 | return RaiseError(QUIC_INVALID_MAX_STREAM_DATA_FRAME_DATA); |
| 3030 | } |
| 3031 | // TODO(fkastenholz): Or should we create a new visitor function, |
| 3032 | // OnMaxStreamDataFrame()? |
| 3033 | if (!visitor_->OnWindowUpdateFrame(frame)) { |
| 3034 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3035 | // Returning true since there was no parsing error. |
| 3036 | return true; |
| 3037 | } |
| 3038 | break; |
| 3039 | } |
| 3040 | case IETF_MAX_STREAMS_BIDIRECTIONAL: |
| 3041 | case IETF_MAX_STREAMS_UNIDIRECTIONAL: { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 3042 | QuicMaxStreamsFrame frame; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3043 | if (!ProcessMaxStreamsFrame(reader, &frame, frame_type)) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 3044 | return RaiseError(QUIC_MAX_STREAMS_DATA); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3045 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 3046 | QUIC_CODE_COUNT_N(quic_max_streams_received, 1, 2); |
| 3047 | if (!visitor_->OnMaxStreamsFrame(frame)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3048 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3049 | // Returning true since there was no parsing error. |
| 3050 | return true; |
| 3051 | } |
| 3052 | break; |
| 3053 | } |
| 3054 | case IETF_PING: { |
| 3055 | // Ping has no payload. |
| 3056 | QuicPingFrame ping_frame; |
| 3057 | if (!visitor_->OnPingFrame(ping_frame)) { |
| 3058 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3059 | // Returning true since there was no parsing error. |
| 3060 | return true; |
| 3061 | } |
| 3062 | break; |
| 3063 | } |
| 3064 | case IETF_BLOCKED: { |
| 3065 | QuicBlockedFrame frame; |
| 3066 | if (!ProcessIetfBlockedFrame(reader, &frame)) { |
| 3067 | return RaiseError(QUIC_INVALID_BLOCKED_DATA); |
| 3068 | } |
| 3069 | if (!visitor_->OnBlockedFrame(frame)) { |
| 3070 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3071 | // Returning true since there was no parsing error. |
| 3072 | return true; |
| 3073 | } |
| 3074 | break; |
| 3075 | } |
| 3076 | case IETF_STREAM_BLOCKED: { |
| 3077 | QuicBlockedFrame frame; |
| 3078 | if (!ProcessStreamBlockedFrame(reader, &frame)) { |
| 3079 | return RaiseError(QUIC_INVALID_STREAM_BLOCKED_DATA); |
| 3080 | } |
| 3081 | if (!visitor_->OnBlockedFrame(frame)) { |
| 3082 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3083 | // Returning true since there was no parsing error. |
| 3084 | return true; |
| 3085 | } |
| 3086 | break; |
| 3087 | } |
| 3088 | case IETF_STREAMS_BLOCKED_UNIDIRECTIONAL: |
| 3089 | case IETF_STREAMS_BLOCKED_BIDIRECTIONAL: { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 3090 | QuicStreamsBlockedFrame frame; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3091 | if (!ProcessStreamsBlockedFrame(reader, &frame, frame_type)) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 3092 | return RaiseError(QUIC_STREAMS_BLOCKED_DATA); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3093 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 3094 | QUIC_CODE_COUNT_N(quic_streams_blocked_received, 1, 2); |
| 3095 | if (!visitor_->OnStreamsBlockedFrame(frame)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3096 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3097 | // Returning true since there was no parsing error. |
| 3098 | return true; |
| 3099 | } |
| 3100 | break; |
| 3101 | } |
| 3102 | case IETF_NEW_CONNECTION_ID: { |
| 3103 | QuicNewConnectionIdFrame frame; |
| 3104 | if (!ProcessNewConnectionIdFrame(reader, &frame)) { |
| 3105 | return RaiseError(QUIC_INVALID_NEW_CONNECTION_ID_DATA); |
| 3106 | } |
| 3107 | if (!visitor_->OnNewConnectionIdFrame(frame)) { |
| 3108 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3109 | // Returning true since there was no parsing error. |
| 3110 | return true; |
| 3111 | } |
| 3112 | break; |
| 3113 | } |
| 3114 | case IETF_RETIRE_CONNECTION_ID: { |
| 3115 | QuicRetireConnectionIdFrame frame; |
| 3116 | if (!ProcessRetireConnectionIdFrame(reader, &frame)) { |
| 3117 | return RaiseError(QUIC_INVALID_RETIRE_CONNECTION_ID_DATA); |
| 3118 | } |
| 3119 | if (!visitor_->OnRetireConnectionIdFrame(frame)) { |
| 3120 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3121 | // Returning true since there was no parsing error. |
| 3122 | return true; |
| 3123 | } |
| 3124 | break; |
| 3125 | } |
| 3126 | case IETF_NEW_TOKEN: { |
| 3127 | QuicNewTokenFrame frame; |
| 3128 | if (!ProcessNewTokenFrame(reader, &frame)) { |
| 3129 | return RaiseError(QUIC_INVALID_NEW_TOKEN); |
| 3130 | } |
| 3131 | if (!visitor_->OnNewTokenFrame(frame)) { |
| 3132 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3133 | // Returning true since there was no parsing error. |
| 3134 | return true; |
| 3135 | } |
| 3136 | break; |
| 3137 | } |
| 3138 | case IETF_STOP_SENDING: { |
| 3139 | QuicStopSendingFrame frame; |
| 3140 | if (!ProcessStopSendingFrame(reader, &frame)) { |
| 3141 | return RaiseError(QUIC_INVALID_STOP_SENDING_FRAME_DATA); |
| 3142 | } |
| 3143 | if (!visitor_->OnStopSendingFrame(frame)) { |
| 3144 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3145 | // Returning true since there was no parsing error. |
| 3146 | return true; |
| 3147 | } |
| 3148 | break; |
| 3149 | } |
| 3150 | case IETF_ACK_ECN: |
| 3151 | case IETF_ACK: { |
| 3152 | QuicAckFrame frame; |
| 3153 | if (!ProcessIetfAckFrame(reader, frame_type, &frame)) { |
| 3154 | return RaiseError(QUIC_INVALID_ACK_DATA); |
| 3155 | } |
| 3156 | break; |
| 3157 | } |
| 3158 | case IETF_PATH_CHALLENGE: { |
| 3159 | QuicPathChallengeFrame frame; |
| 3160 | if (!ProcessPathChallengeFrame(reader, &frame)) { |
| 3161 | return RaiseError(QUIC_INVALID_PATH_CHALLENGE_DATA); |
| 3162 | } |
| 3163 | if (!visitor_->OnPathChallengeFrame(frame)) { |
| 3164 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3165 | // Returning true since there was no parsing error. |
| 3166 | return true; |
| 3167 | } |
| 3168 | break; |
| 3169 | } |
| 3170 | case IETF_PATH_RESPONSE: { |
| 3171 | QuicPathResponseFrame frame; |
| 3172 | if (!ProcessPathResponseFrame(reader, &frame)) { |
| 3173 | return RaiseError(QUIC_INVALID_PATH_RESPONSE_DATA); |
| 3174 | } |
| 3175 | if (!visitor_->OnPathResponseFrame(frame)) { |
| 3176 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3177 | // Returning true since there was no parsing error. |
| 3178 | return true; |
| 3179 | } |
| 3180 | break; |
| 3181 | } |
| 3182 | case IETF_EXTENSION_MESSAGE_NO_LENGTH: |
| 3183 | QUIC_FALLTHROUGH_INTENDED; |
| 3184 | case IETF_EXTENSION_MESSAGE: { |
| 3185 | QuicMessageFrame message_frame; |
| 3186 | if (!ProcessMessageFrame( |
| 3187 | reader, frame_type == IETF_EXTENSION_MESSAGE_NO_LENGTH, |
| 3188 | &message_frame)) { |
| 3189 | return RaiseError(QUIC_INVALID_MESSAGE_DATA); |
| 3190 | } |
| 3191 | if (!visitor_->OnMessageFrame(message_frame)) { |
| 3192 | QUIC_DVLOG(1) << ENDPOINT |
| 3193 | << "Visitor asked to stop further processing."; |
| 3194 | // Returning true since there was no parsing error. |
| 3195 | return true; |
| 3196 | } |
| 3197 | break; |
| 3198 | } |
| 3199 | case IETF_CRYPTO: { |
| 3200 | QuicCryptoFrame frame; |
| 3201 | if (!ProcessCryptoFrame(reader, &frame)) { |
| 3202 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 3203 | } |
| 3204 | if (!visitor_->OnCryptoFrame(frame)) { |
| 3205 | QUIC_DVLOG(1) << "Visitor asked to stop further processing."; |
| 3206 | // Returning true since there was no parsing error. |
| 3207 | return true; |
| 3208 | } |
| 3209 | break; |
| 3210 | } |
| 3211 | |
| 3212 | default: |
| 3213 | set_detailed_error("Illegal frame type."); |
| 3214 | QUIC_DLOG(WARNING) |
| 3215 | << ENDPOINT |
| 3216 | << "Illegal frame type: " << static_cast<int>(frame_type); |
| 3217 | return RaiseError(QUIC_INVALID_FRAME_DATA); |
| 3218 | } |
| 3219 | } |
| 3220 | } |
| 3221 | return true; |
| 3222 | } |
| 3223 | |
| 3224 | namespace { |
| 3225 | // Create a mask that sets the last |num_bits| to 1 and the rest to 0. |
| 3226 | inline uint8_t GetMaskFromNumBits(uint8_t num_bits) { |
| 3227 | return (1u << num_bits) - 1; |
| 3228 | } |
| 3229 | |
| 3230 | // Extract |num_bits| from |flags| offset by |offset|. |
| 3231 | uint8_t ExtractBits(uint8_t flags, uint8_t num_bits, uint8_t offset) { |
| 3232 | return (flags >> offset) & GetMaskFromNumBits(num_bits); |
| 3233 | } |
| 3234 | |
| 3235 | // Extract the bit at position |offset| from |flags| as a bool. |
| 3236 | bool ExtractBit(uint8_t flags, uint8_t offset) { |
| 3237 | return ((flags >> offset) & GetMaskFromNumBits(1)) != 0; |
| 3238 | } |
| 3239 | |
| 3240 | // Set |num_bits|, offset by |offset| to |val| in |flags|. |
| 3241 | void SetBits(uint8_t* flags, uint8_t val, uint8_t num_bits, uint8_t offset) { |
| 3242 | DCHECK_LE(val, GetMaskFromNumBits(num_bits)); |
| 3243 | *flags |= val << offset; |
| 3244 | } |
| 3245 | |
| 3246 | // Set the bit at position |offset| to |val| in |flags|. |
| 3247 | void SetBit(uint8_t* flags, bool val, uint8_t offset) { |
| 3248 | SetBits(flags, val ? 1 : 0, 1, offset); |
| 3249 | } |
| 3250 | } // namespace |
| 3251 | |
| 3252 | bool QuicFramer::ProcessStreamFrame(QuicDataReader* reader, |
| 3253 | uint8_t frame_type, |
| 3254 | QuicStreamFrame* frame) { |
| 3255 | uint8_t stream_flags = frame_type; |
| 3256 | |
| 3257 | uint8_t stream_id_length = 0; |
| 3258 | uint8_t offset_length = 4; |
| 3259 | bool has_data_length = true; |
| 3260 | stream_flags &= ~kQuicFrameTypeStreamMask; |
| 3261 | |
| 3262 | // Read from right to left: StreamID, Offset, Data Length, Fin. |
| 3263 | stream_id_length = (stream_flags & kQuicStreamIDLengthMask) + 1; |
| 3264 | stream_flags >>= kQuicStreamIdShift; |
| 3265 | |
| 3266 | offset_length = (stream_flags & kQuicStreamOffsetMask); |
| 3267 | // There is no encoding for 1 byte, only 0 and 2 through 8. |
| 3268 | if (offset_length > 0) { |
| 3269 | offset_length += 1; |
| 3270 | } |
| 3271 | stream_flags >>= kQuicStreamShift; |
| 3272 | |
| 3273 | has_data_length = |
| 3274 | (stream_flags & kQuicStreamDataLengthMask) == kQuicStreamDataLengthMask; |
| 3275 | stream_flags >>= kQuicStreamDataLengthShift; |
| 3276 | |
| 3277 | frame->fin = (stream_flags & kQuicStreamFinMask) == kQuicStreamFinShift; |
| 3278 | |
| 3279 | uint64_t stream_id; |
| 3280 | if (!reader->ReadBytesToUInt64(stream_id_length, &stream_id)) { |
| 3281 | set_detailed_error("Unable to read stream_id."); |
| 3282 | return false; |
| 3283 | } |
| 3284 | frame->stream_id = static_cast<QuicStreamId>(stream_id); |
| 3285 | |
| 3286 | if (!reader->ReadBytesToUInt64(offset_length, &frame->offset)) { |
| 3287 | set_detailed_error("Unable to read offset."); |
| 3288 | return false; |
| 3289 | } |
| 3290 | |
| 3291 | // TODO(ianswett): Don't use QuicStringPiece as an intermediary. |
| 3292 | QuicStringPiece data; |
| 3293 | if (has_data_length) { |
| 3294 | if (!reader->ReadStringPiece16(&data)) { |
| 3295 | set_detailed_error("Unable to read frame data."); |
| 3296 | return false; |
| 3297 | } |
| 3298 | } else { |
| 3299 | if (!reader->ReadStringPiece(&data, reader->BytesRemaining())) { |
| 3300 | set_detailed_error("Unable to read frame data."); |
| 3301 | return false; |
| 3302 | } |
| 3303 | } |
| 3304 | frame->data_buffer = data.data(); |
| 3305 | frame->data_length = static_cast<uint16_t>(data.length()); |
| 3306 | |
| 3307 | return true; |
| 3308 | } |
| 3309 | |
| 3310 | bool QuicFramer::ProcessIetfStreamFrame(QuicDataReader* reader, |
| 3311 | uint8_t frame_type, |
| 3312 | QuicStreamFrame* frame) { |
| 3313 | // Read stream id from the frame. It's always present. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 3314 | if (!reader->ReadVarIntU32(&frame->stream_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3315 | set_detailed_error("Unable to read stream_id."); |
| 3316 | return false; |
| 3317 | } |
| 3318 | |
| 3319 | // If we have a data offset, read it. If not, set to 0. |
| 3320 | if (frame_type & IETF_STREAM_FRAME_OFF_BIT) { |
| 3321 | if (!reader->ReadVarInt62(&frame->offset)) { |
| 3322 | set_detailed_error("Unable to read stream data offset."); |
| 3323 | return false; |
| 3324 | } |
| 3325 | } else { |
| 3326 | // no offset in the frame, ensure it's 0 in the Frame. |
| 3327 | frame->offset = 0; |
| 3328 | } |
| 3329 | |
| 3330 | // If we have a data length, read it. If not, set to 0. |
| 3331 | if (frame_type & IETF_STREAM_FRAME_LEN_BIT) { |
| 3332 | QuicIetfStreamDataLength length; |
| 3333 | if (!reader->ReadVarInt62(&length)) { |
| 3334 | set_detailed_error("Unable to read stream data length."); |
| 3335 | return false; |
| 3336 | } |
| 3337 | if (length > 0xffff) { |
| 3338 | set_detailed_error("Stream data length is too large."); |
| 3339 | return false; |
| 3340 | } |
| 3341 | frame->data_length = length; |
| 3342 | } else { |
| 3343 | // no length in the frame, it is the number of bytes remaining in the |
| 3344 | // packet. |
| 3345 | frame->data_length = reader->BytesRemaining(); |
| 3346 | } |
| 3347 | |
| 3348 | if (frame_type & IETF_STREAM_FRAME_FIN_BIT) { |
| 3349 | frame->fin = true; |
| 3350 | } else { |
| 3351 | frame->fin = false; |
| 3352 | } |
| 3353 | |
| 3354 | // TODO(ianswett): Don't use QuicStringPiece as an intermediary. |
| 3355 | QuicStringPiece data; |
| 3356 | if (!reader->ReadStringPiece(&data, frame->data_length)) { |
| 3357 | set_detailed_error("Unable to read frame data."); |
| 3358 | return false; |
| 3359 | } |
| 3360 | frame->data_buffer = data.data(); |
| 3361 | frame->data_length = static_cast<QuicIetfStreamDataLength>(data.length()); |
| 3362 | |
| 3363 | return true; |
| 3364 | } |
| 3365 | |
| 3366 | bool QuicFramer::ProcessCryptoFrame(QuicDataReader* reader, |
| 3367 | QuicCryptoFrame* frame) { |
| 3368 | if (!reader->ReadVarInt62(&frame->offset)) { |
| 3369 | set_detailed_error("Unable to read crypto data offset."); |
| 3370 | return false; |
| 3371 | } |
| 3372 | uint64_t len; |
| 3373 | if (!reader->ReadVarInt62(&len) || |
| 3374 | len > std::numeric_limits<QuicPacketLength>::max()) { |
| 3375 | set_detailed_error("Invalid data length."); |
| 3376 | return false; |
| 3377 | } |
| 3378 | frame->data_length = len; |
| 3379 | |
| 3380 | // TODO(ianswett): Don't use QuicStringPiece as an intermediary. |
| 3381 | QuicStringPiece data; |
| 3382 | if (!reader->ReadStringPiece(&data, frame->data_length)) { |
| 3383 | set_detailed_error("Unable to read frame data."); |
| 3384 | return false; |
| 3385 | } |
| 3386 | frame->data_buffer = data.data(); |
| 3387 | return true; |
| 3388 | } |
| 3389 | |
| 3390 | bool QuicFramer::ProcessAckFrame(QuicDataReader* reader, uint8_t frame_type) { |
| 3391 | const bool has_ack_blocks = |
| 3392 | ExtractBit(frame_type, kQuicHasMultipleAckBlocksOffset); |
| 3393 | uint8_t num_ack_blocks = 0; |
| 3394 | uint8_t num_received_packets = 0; |
| 3395 | |
| 3396 | // Determine the two lengths from the frame type: largest acked length, |
| 3397 | // ack block length. |
| 3398 | const QuicPacketNumberLength ack_block_length = ReadAckPacketNumberLength( |
| 3399 | version_.transport_version, |
| 3400 | ExtractBits(frame_type, kQuicSequenceNumberLengthNumBits, |
| 3401 | kActBlockLengthOffset)); |
| 3402 | const QuicPacketNumberLength largest_acked_length = ReadAckPacketNumberLength( |
| 3403 | version_.transport_version, |
| 3404 | ExtractBits(frame_type, kQuicSequenceNumberLengthNumBits, |
| 3405 | kLargestAckedOffset)); |
| 3406 | |
| 3407 | uint64_t largest_acked; |
| 3408 | if (!reader->ReadBytesToUInt64(largest_acked_length, &largest_acked)) { |
| 3409 | set_detailed_error("Unable to read largest acked."); |
| 3410 | return false; |
| 3411 | } |
| 3412 | |
| 3413 | if (largest_acked < first_sending_packet_number_.ToUint64()) { |
| 3414 | // Connection always sends packet starting from kFirstSendingPacketNumber > |
| 3415 | // 0, peer has observed an unsent packet. |
| 3416 | set_detailed_error("Largest acked is 0."); |
| 3417 | return false; |
| 3418 | } |
| 3419 | |
| 3420 | uint64_t ack_delay_time_us; |
| 3421 | if (!reader->ReadUFloat16(&ack_delay_time_us)) { |
| 3422 | set_detailed_error("Unable to read ack delay time."); |
| 3423 | return false; |
| 3424 | } |
| 3425 | |
| 3426 | if (!visitor_->OnAckFrameStart( |
| 3427 | QuicPacketNumber(largest_acked), |
| 3428 | ack_delay_time_us == kUFloat16MaxValue |
| 3429 | ? QuicTime::Delta::Infinite() |
| 3430 | : QuicTime::Delta::FromMicroseconds(ack_delay_time_us))) { |
| 3431 | // The visitor suppresses further processing of the packet. Although this is |
| 3432 | // not a parsing error, returns false as this is in middle of processing an |
| 3433 | // ack frame, |
| 3434 | set_detailed_error("Visitor suppresses further processing of ack frame."); |
| 3435 | return false; |
| 3436 | } |
| 3437 | |
| 3438 | if (has_ack_blocks && !reader->ReadUInt8(&num_ack_blocks)) { |
| 3439 | set_detailed_error("Unable to read num of ack blocks."); |
| 3440 | return false; |
| 3441 | } |
| 3442 | |
| 3443 | uint64_t first_block_length; |
| 3444 | if (!reader->ReadBytesToUInt64(ack_block_length, &first_block_length)) { |
| 3445 | set_detailed_error("Unable to read first ack block length."); |
| 3446 | return false; |
| 3447 | } |
| 3448 | |
| 3449 | if (first_block_length == 0) { |
| 3450 | set_detailed_error("First block length is zero."); |
| 3451 | return false; |
| 3452 | } |
| 3453 | bool first_ack_block_underflow = first_block_length > largest_acked + 1; |
| 3454 | if (first_block_length + first_sending_packet_number_.ToUint64() > |
| 3455 | largest_acked + 1) { |
| 3456 | first_ack_block_underflow = true; |
| 3457 | } |
| 3458 | if (first_ack_block_underflow) { |
| 3459 | set_detailed_error(QuicStrCat("Underflow with first ack block length ", |
| 3460 | first_block_length, " largest acked is ", |
| 3461 | largest_acked, ".") |
| 3462 | .c_str()); |
| 3463 | return false; |
| 3464 | } |
| 3465 | |
| 3466 | uint64_t first_received = largest_acked + 1 - first_block_length; |
| 3467 | if (!visitor_->OnAckRange(QuicPacketNumber(first_received), |
| 3468 | QuicPacketNumber(largest_acked + 1))) { |
| 3469 | // The visitor suppresses further processing of the packet. Although |
| 3470 | // this is not a parsing error, returns false as this is in middle |
| 3471 | // of processing an ack frame, |
| 3472 | set_detailed_error("Visitor suppresses further processing of ack frame."); |
| 3473 | return false; |
| 3474 | } |
| 3475 | |
| 3476 | if (num_ack_blocks > 0) { |
| 3477 | for (size_t i = 0; i < num_ack_blocks; ++i) { |
| 3478 | uint8_t gap = 0; |
| 3479 | if (!reader->ReadUInt8(&gap)) { |
| 3480 | set_detailed_error("Unable to read gap to next ack block."); |
| 3481 | return false; |
| 3482 | } |
| 3483 | uint64_t current_block_length; |
| 3484 | if (!reader->ReadBytesToUInt64(ack_block_length, ¤t_block_length)) { |
| 3485 | set_detailed_error("Unable to ack block length."); |
| 3486 | return false; |
| 3487 | } |
| 3488 | bool ack_block_underflow = first_received < gap + current_block_length; |
| 3489 | if (first_received < gap + current_block_length + |
| 3490 | first_sending_packet_number_.ToUint64()) { |
| 3491 | ack_block_underflow = true; |
| 3492 | } |
| 3493 | if (ack_block_underflow) { |
| 3494 | set_detailed_error( |
| 3495 | QuicStrCat("Underflow with ack block length ", current_block_length, |
| 3496 | ", end of block is ", first_received - gap, ".") |
| 3497 | .c_str()); |
| 3498 | return false; |
| 3499 | } |
| 3500 | |
| 3501 | first_received -= (gap + current_block_length); |
| 3502 | if (current_block_length > 0) { |
| 3503 | if (!visitor_->OnAckRange( |
| 3504 | QuicPacketNumber(first_received), |
| 3505 | QuicPacketNumber(first_received) + current_block_length)) { |
| 3506 | // The visitor suppresses further processing of the packet. Although |
| 3507 | // this is not a parsing error, returns false as this is in middle |
| 3508 | // of processing an ack frame, |
| 3509 | set_detailed_error( |
| 3510 | "Visitor suppresses further processing of ack frame."); |
| 3511 | return false; |
| 3512 | } |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | if (!reader->ReadUInt8(&num_received_packets)) { |
| 3518 | set_detailed_error("Unable to read num received packets."); |
| 3519 | return false; |
| 3520 | } |
| 3521 | |
| 3522 | if (!ProcessTimestampsInAckFrame(num_received_packets, |
| 3523 | QuicPacketNumber(largest_acked), reader)) { |
| 3524 | return false; |
| 3525 | } |
| 3526 | |
| 3527 | // Done processing the ACK frame. |
| 3528 | return visitor_->OnAckFrameEnd(QuicPacketNumber(first_received)); |
| 3529 | } |
| 3530 | |
| 3531 | bool QuicFramer::ProcessTimestampsInAckFrame(uint8_t num_received_packets, |
| 3532 | QuicPacketNumber largest_acked, |
| 3533 | QuicDataReader* reader) { |
| 3534 | if (num_received_packets == 0) { |
| 3535 | return true; |
| 3536 | } |
| 3537 | uint8_t delta_from_largest_observed; |
| 3538 | if (!reader->ReadUInt8(&delta_from_largest_observed)) { |
| 3539 | set_detailed_error("Unable to read sequence delta in received packets."); |
| 3540 | return false; |
| 3541 | } |
| 3542 | |
| 3543 | if (largest_acked.ToUint64() <= delta_from_largest_observed) { |
| 3544 | set_detailed_error(QuicStrCat("delta_from_largest_observed too high: ", |
| 3545 | delta_from_largest_observed, |
| 3546 | ", largest_acked: ", largest_acked.ToUint64()) |
| 3547 | .c_str()); |
| 3548 | return false; |
| 3549 | } |
| 3550 | |
| 3551 | // Time delta from the framer creation. |
| 3552 | uint32_t time_delta_us; |
| 3553 | if (!reader->ReadUInt32(&time_delta_us)) { |
| 3554 | set_detailed_error("Unable to read time delta in received packets."); |
| 3555 | return false; |
| 3556 | } |
| 3557 | |
| 3558 | QuicPacketNumber seq_num = largest_acked - delta_from_largest_observed; |
| 3559 | if (process_timestamps_) { |
| 3560 | last_timestamp_ = CalculateTimestampFromWire(time_delta_us); |
| 3561 | |
| 3562 | visitor_->OnAckTimestamp(seq_num, creation_time_ + last_timestamp_); |
| 3563 | } |
| 3564 | |
| 3565 | for (uint8_t i = 1; i < num_received_packets; ++i) { |
| 3566 | if (!reader->ReadUInt8(&delta_from_largest_observed)) { |
| 3567 | set_detailed_error("Unable to read sequence delta in received packets."); |
| 3568 | return false; |
| 3569 | } |
| 3570 | if (largest_acked.ToUint64() <= delta_from_largest_observed) { |
| 3571 | set_detailed_error( |
| 3572 | QuicStrCat("delta_from_largest_observed too high: ", |
| 3573 | delta_from_largest_observed, |
| 3574 | ", largest_acked: ", largest_acked.ToUint64()) |
| 3575 | .c_str()); |
| 3576 | return false; |
| 3577 | } |
| 3578 | seq_num = largest_acked - delta_from_largest_observed; |
| 3579 | |
| 3580 | // Time delta from the previous timestamp. |
| 3581 | uint64_t incremental_time_delta_us; |
| 3582 | if (!reader->ReadUFloat16(&incremental_time_delta_us)) { |
| 3583 | set_detailed_error( |
| 3584 | "Unable to read incremental time delta in received packets."); |
| 3585 | return false; |
| 3586 | } |
| 3587 | |
| 3588 | if (process_timestamps_) { |
| 3589 | last_timestamp_ = last_timestamp_ + QuicTime::Delta::FromMicroseconds( |
| 3590 | incremental_time_delta_us); |
| 3591 | visitor_->OnAckTimestamp(seq_num, creation_time_ + last_timestamp_); |
| 3592 | } |
| 3593 | } |
| 3594 | return true; |
| 3595 | } |
| 3596 | |
| 3597 | bool QuicFramer::ProcessIetfAckFrame(QuicDataReader* reader, |
| 3598 | uint64_t frame_type, |
| 3599 | QuicAckFrame* ack_frame) { |
| 3600 | uint64_t largest_acked; |
| 3601 | if (!reader->ReadVarInt62(&largest_acked)) { |
| 3602 | set_detailed_error("Unable to read largest acked."); |
| 3603 | return false; |
| 3604 | } |
| 3605 | if (largest_acked < first_sending_packet_number_.ToUint64()) { |
| 3606 | // Connection always sends packet starting from kFirstSendingPacketNumber > |
| 3607 | // 0, peer has observed an unsent packet. |
| 3608 | set_detailed_error("Largest acked is 0."); |
| 3609 | return false; |
| 3610 | } |
| 3611 | ack_frame->largest_acked = static_cast<QuicPacketNumber>(largest_acked); |
| 3612 | uint64_t ack_delay_time_in_us; |
| 3613 | if (!reader->ReadVarInt62(&ack_delay_time_in_us)) { |
| 3614 | set_detailed_error("Unable to read ack delay time."); |
| 3615 | return false; |
| 3616 | } |
| 3617 | |
| 3618 | // TODO(fkastenholz) when we get real IETF QUIC, need to get |
| 3619 | // the currect shift from the transport parameters. |
| 3620 | if (ack_delay_time_in_us == kVarInt62MaxValue) { |
| 3621 | ack_frame->ack_delay_time = QuicTime::Delta::Infinite(); |
| 3622 | } else { |
| 3623 | ack_delay_time_in_us = (ack_delay_time_in_us << kIetfAckTimestampShift); |
| 3624 | ack_frame->ack_delay_time = |
| 3625 | QuicTime::Delta::FromMicroseconds(ack_delay_time_in_us); |
| 3626 | } |
| 3627 | if (frame_type == IETF_ACK_ECN) { |
| 3628 | ack_frame->ecn_counters_populated = true; |
| 3629 | if (!reader->ReadVarInt62(&ack_frame->ect_0_count)) { |
| 3630 | set_detailed_error("Unable to read ack ect_0_count."); |
| 3631 | return false; |
| 3632 | } |
| 3633 | if (!reader->ReadVarInt62(&ack_frame->ect_1_count)) { |
| 3634 | set_detailed_error("Unable to read ack ect_1_count."); |
| 3635 | return false; |
| 3636 | } |
| 3637 | if (!reader->ReadVarInt62(&ack_frame->ecn_ce_count)) { |
| 3638 | set_detailed_error("Unable to read ack ecn_ce_count."); |
| 3639 | return false; |
| 3640 | } |
| 3641 | } else { |
| 3642 | ack_frame->ecn_counters_populated = false; |
| 3643 | ack_frame->ect_0_count = 0; |
| 3644 | ack_frame->ect_1_count = 0; |
| 3645 | ack_frame->ecn_ce_count = 0; |
| 3646 | } |
| 3647 | if (!visitor_->OnAckFrameStart(QuicPacketNumber(largest_acked), |
| 3648 | ack_frame->ack_delay_time)) { |
| 3649 | // The visitor suppresses further processing of the packet. Although this is |
| 3650 | // not a parsing error, returns false as this is in middle of processing an |
| 3651 | // ACK frame. |
| 3652 | set_detailed_error("Visitor suppresses further processing of ACK frame."); |
| 3653 | return false; |
| 3654 | } |
| 3655 | |
| 3656 | // Get number of ACK blocks from the packet. |
| 3657 | uint64_t ack_block_count; |
| 3658 | if (!reader->ReadVarInt62(&ack_block_count)) { |
| 3659 | set_detailed_error("Unable to read ack block count."); |
| 3660 | return false; |
| 3661 | } |
| 3662 | // There always is a first ACK block, which is the (number of packets being |
| 3663 | // acked)-1, up to and including the packet at largest_acked. Therefore if the |
| 3664 | // value is 0, then only largest is acked. If it is 1, then largest-1, |
| 3665 | // largest] are acked, etc |
| 3666 | uint64_t ack_block_value; |
| 3667 | if (!reader->ReadVarInt62(&ack_block_value)) { |
| 3668 | set_detailed_error("Unable to read first ack block length."); |
| 3669 | return false; |
| 3670 | } |
| 3671 | // Calculate the packets being acked in the first block. |
| 3672 | // +1 because AddRange implementation requires [low,high) |
| 3673 | uint64_t block_high = largest_acked + 1; |
| 3674 | uint64_t block_low = largest_acked - ack_block_value; |
| 3675 | |
| 3676 | // ack_block_value is the number of packets preceding the |
| 3677 | // largest_acked packet which are in the block being acked. Thus, |
| 3678 | // its maximum value is largest_acked-1. Test this, reporting an |
| 3679 | // error if the value is wrong. |
| 3680 | if (ack_block_value + first_sending_packet_number_.ToUint64() > |
| 3681 | largest_acked) { |
| 3682 | set_detailed_error(QuicStrCat("Underflow with first ack block length ", |
| 3683 | ack_block_value + 1, " largest acked is ", |
| 3684 | largest_acked, ".") |
| 3685 | .c_str()); |
| 3686 | return false; |
| 3687 | } |
| 3688 | |
| 3689 | if (!visitor_->OnAckRange(QuicPacketNumber(block_low), |
| 3690 | QuicPacketNumber(block_high))) { |
| 3691 | // The visitor suppresses further processing of the packet. Although |
| 3692 | // this is not a parsing error, returns false as this is in middle |
| 3693 | // of processing an ACK frame. |
| 3694 | set_detailed_error("Visitor suppresses further processing of ACK frame."); |
| 3695 | return false; |
| 3696 | } |
| 3697 | |
| 3698 | while (ack_block_count != 0) { |
| 3699 | uint64_t gap_block_value; |
| 3700 | // Get the sizes of the gap and ack blocks, |
| 3701 | if (!reader->ReadVarInt62(&gap_block_value)) { |
| 3702 | set_detailed_error("Unable to read gap block value."); |
| 3703 | return false; |
| 3704 | } |
| 3705 | // It's an error if the gap is larger than the space from packet |
| 3706 | // number 0 to the start of the block that's just been acked, PLUS |
| 3707 | // there must be space for at least 1 packet to be acked. For |
| 3708 | // example, if block_low is 10 and gap_block_value is 9, it means |
| 3709 | // the gap block is 10 packets long, leaving no room for a packet |
| 3710 | // to be acked. Thus, gap_block_value+2 can not be larger than |
| 3711 | // block_low. |
| 3712 | // The test is written this way to detect wrap-arounds. |
| 3713 | if ((gap_block_value + 2) > block_low) { |
| 3714 | set_detailed_error( |
| 3715 | QuicStrCat("Underflow with gap block length ", gap_block_value + 1, |
| 3716 | " previous ack block start is ", block_low, ".") |
| 3717 | .c_str()); |
| 3718 | return false; |
| 3719 | } |
| 3720 | |
| 3721 | // Adjust block_high to be the top of the next ack block. |
| 3722 | // There is a gap of |gap_block_value| packets between the bottom |
| 3723 | // of ack block N and top of block N+1. Note that gap_block_value |
| 3724 | // is he size of the gap minus 1 (per the QUIC protocol), and |
| 3725 | // block_high is the packet number of the first packet of the gap |
| 3726 | // (per the implementation of OnAckRange/AddAckRange, below). |
| 3727 | block_high = block_low - 1 - gap_block_value; |
| 3728 | |
| 3729 | if (!reader->ReadVarInt62(&ack_block_value)) { |
| 3730 | set_detailed_error("Unable to read ack block value."); |
| 3731 | return false; |
| 3732 | } |
| 3733 | if (ack_block_value + first_sending_packet_number_.ToUint64() > |
| 3734 | (block_high - 1)) { |
| 3735 | set_detailed_error( |
| 3736 | QuicStrCat("Underflow with ack block length ", ack_block_value + 1, |
| 3737 | " latest ack block end is ", block_high - 1, ".") |
| 3738 | .c_str()); |
| 3739 | return false; |
| 3740 | } |
| 3741 | // Calculate the low end of the new nth ack block. The +1 is |
| 3742 | // because the encoded value is the blocksize-1. |
| 3743 | block_low = block_high - 1 - ack_block_value; |
| 3744 | if (!visitor_->OnAckRange(QuicPacketNumber(block_low), |
| 3745 | QuicPacketNumber(block_high))) { |
| 3746 | // The visitor suppresses further processing of the packet. Although |
| 3747 | // this is not a parsing error, returns false as this is in middle |
| 3748 | // of processing an ACK frame. |
| 3749 | set_detailed_error("Visitor suppresses further processing of ACK frame."); |
| 3750 | return false; |
| 3751 | } |
| 3752 | |
| 3753 | // Another one done. |
| 3754 | ack_block_count--; |
| 3755 | } |
| 3756 | |
| 3757 | return visitor_->OnAckFrameEnd(QuicPacketNumber(block_low)); |
| 3758 | } |
| 3759 | |
| 3760 | bool QuicFramer::ProcessStopWaitingFrame(QuicDataReader* reader, |
| 3761 | const QuicPacketHeader& header, |
| 3762 | QuicStopWaitingFrame* stop_waiting) { |
| 3763 | uint64_t least_unacked_delta; |
| 3764 | if (!reader->ReadBytesToUInt64(header.packet_number_length, |
| 3765 | &least_unacked_delta)) { |
| 3766 | set_detailed_error("Unable to read least unacked delta."); |
| 3767 | return false; |
| 3768 | } |
| 3769 | if (header.packet_number.ToUint64() <= least_unacked_delta) { |
| 3770 | set_detailed_error("Invalid unacked delta."); |
| 3771 | return false; |
| 3772 | } |
| 3773 | stop_waiting->least_unacked = header.packet_number - least_unacked_delta; |
| 3774 | |
| 3775 | return true; |
| 3776 | } |
| 3777 | |
| 3778 | bool QuicFramer::ProcessRstStreamFrame(QuicDataReader* reader, |
| 3779 | QuicRstStreamFrame* frame) { |
| 3780 | if (!reader->ReadUInt32(&frame->stream_id)) { |
| 3781 | set_detailed_error("Unable to read stream_id."); |
| 3782 | return false; |
| 3783 | } |
| 3784 | |
| 3785 | if (!reader->ReadUInt64(&frame->byte_offset)) { |
| 3786 | set_detailed_error("Unable to read rst stream sent byte offset."); |
| 3787 | return false; |
| 3788 | } |
| 3789 | |
| 3790 | uint32_t error_code; |
| 3791 | if (!reader->ReadUInt32(&error_code)) { |
| 3792 | set_detailed_error("Unable to read rst stream error code."); |
| 3793 | return false; |
| 3794 | } |
| 3795 | |
| 3796 | if (error_code >= QUIC_STREAM_LAST_ERROR) { |
| 3797 | // Ignore invalid stream error code if any. |
| 3798 | error_code = QUIC_STREAM_LAST_ERROR; |
| 3799 | } |
| 3800 | |
| 3801 | frame->error_code = static_cast<QuicRstStreamErrorCode>(error_code); |
| 3802 | |
| 3803 | return true; |
| 3804 | } |
| 3805 | |
| 3806 | bool QuicFramer::ProcessConnectionCloseFrame(QuicDataReader* reader, |
| 3807 | QuicConnectionCloseFrame* frame) { |
| 3808 | uint32_t error_code; |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 3809 | frame->close_type = GOOGLE_QUIC_CONNECTION_CLOSE; |
| 3810 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3811 | if (!reader->ReadUInt32(&error_code)) { |
| 3812 | set_detailed_error("Unable to read connection close error code."); |
| 3813 | return false; |
| 3814 | } |
| 3815 | |
| 3816 | if (error_code >= QUIC_LAST_ERROR) { |
| 3817 | // Ignore invalid QUIC error code if any. |
| 3818 | error_code = QUIC_LAST_ERROR; |
| 3819 | } |
| 3820 | |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 3821 | frame->quic_error_code = static_cast<QuicErrorCode>(error_code); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3822 | |
| 3823 | QuicStringPiece error_details; |
| 3824 | if (!reader->ReadStringPiece16(&error_details)) { |
| 3825 | set_detailed_error("Unable to read connection close error details."); |
| 3826 | return false; |
| 3827 | } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 3828 | frame->error_details = std::string(error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3829 | |
| 3830 | return true; |
| 3831 | } |
| 3832 | |
| 3833 | bool QuicFramer::ProcessGoAwayFrame(QuicDataReader* reader, |
| 3834 | QuicGoAwayFrame* frame) { |
| 3835 | uint32_t error_code; |
| 3836 | if (!reader->ReadUInt32(&error_code)) { |
| 3837 | set_detailed_error("Unable to read go away error code."); |
| 3838 | return false; |
| 3839 | } |
| 3840 | |
| 3841 | if (error_code >= QUIC_LAST_ERROR) { |
| 3842 | // Ignore invalid QUIC error code if any. |
| 3843 | error_code = QUIC_LAST_ERROR; |
| 3844 | } |
| 3845 | frame->error_code = static_cast<QuicErrorCode>(error_code); |
| 3846 | |
| 3847 | uint32_t stream_id; |
| 3848 | if (!reader->ReadUInt32(&stream_id)) { |
| 3849 | set_detailed_error("Unable to read last good stream id."); |
| 3850 | return false; |
| 3851 | } |
| 3852 | frame->last_good_stream_id = static_cast<QuicStreamId>(stream_id); |
| 3853 | |
| 3854 | QuicStringPiece reason_phrase; |
| 3855 | if (!reader->ReadStringPiece16(&reason_phrase)) { |
| 3856 | set_detailed_error("Unable to read goaway reason."); |
| 3857 | return false; |
| 3858 | } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 3859 | frame->reason_phrase = std::string(reason_phrase); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3860 | |
| 3861 | return true; |
| 3862 | } |
| 3863 | |
| 3864 | bool QuicFramer::ProcessWindowUpdateFrame(QuicDataReader* reader, |
| 3865 | QuicWindowUpdateFrame* frame) { |
| 3866 | if (!reader->ReadUInt32(&frame->stream_id)) { |
| 3867 | set_detailed_error("Unable to read stream_id."); |
| 3868 | return false; |
| 3869 | } |
| 3870 | |
| 3871 | if (!reader->ReadUInt64(&frame->byte_offset)) { |
| 3872 | set_detailed_error("Unable to read window byte_offset."); |
| 3873 | return false; |
| 3874 | } |
| 3875 | |
| 3876 | return true; |
| 3877 | } |
| 3878 | |
| 3879 | bool QuicFramer::ProcessBlockedFrame(QuicDataReader* reader, |
| 3880 | QuicBlockedFrame* frame) { |
| 3881 | DCHECK_NE(QUIC_VERSION_99, version_.transport_version) |
| 3882 | << "Attempt to process non-IETF frames but version is 99"; |
| 3883 | |
| 3884 | if (!reader->ReadUInt32(&frame->stream_id)) { |
| 3885 | set_detailed_error("Unable to read stream_id."); |
| 3886 | return false; |
| 3887 | } |
| 3888 | |
| 3889 | return true; |
| 3890 | } |
| 3891 | |
| 3892 | void QuicFramer::ProcessPaddingFrame(QuicDataReader* reader, |
| 3893 | QuicPaddingFrame* frame) { |
| 3894 | // Type byte has been read. |
| 3895 | frame->num_padding_bytes = 1; |
| 3896 | uint8_t next_byte; |
| 3897 | while (!reader->IsDoneReading() && reader->PeekByte() == 0x00) { |
| 3898 | reader->ReadBytes(&next_byte, 1); |
| 3899 | DCHECK_EQ(0x00, next_byte); |
| 3900 | ++frame->num_padding_bytes; |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | bool QuicFramer::ProcessMessageFrame(QuicDataReader* reader, |
| 3905 | bool no_message_length, |
| 3906 | QuicMessageFrame* frame) { |
| 3907 | if (no_message_length) { |
| 3908 | QuicStringPiece remaining(reader->ReadRemainingPayload()); |
| 3909 | frame->data = remaining.data(); |
| 3910 | frame->message_length = remaining.length(); |
| 3911 | return true; |
| 3912 | } |
| 3913 | |
| 3914 | uint64_t message_length; |
| 3915 | if (!reader->ReadVarInt62(&message_length)) { |
| 3916 | set_detailed_error("Unable to read message length"); |
| 3917 | return false; |
| 3918 | } |
| 3919 | |
| 3920 | QuicStringPiece message_piece; |
| 3921 | if (!reader->ReadStringPiece(&message_piece, message_length)) { |
| 3922 | set_detailed_error("Unable to read message data"); |
| 3923 | return false; |
| 3924 | } |
| 3925 | |
| 3926 | frame->data = message_piece.data(); |
| 3927 | frame->message_length = message_length; |
| 3928 | |
| 3929 | return true; |
| 3930 | } |
| 3931 | |
| 3932 | // static |
| 3933 | QuicStringPiece QuicFramer::GetAssociatedDataFromEncryptedPacket( |
| 3934 | QuicTransportVersion version, |
| 3935 | const QuicEncryptedPacket& encrypted, |
| 3936 | QuicConnectionIdLength destination_connection_id_length, |
| 3937 | QuicConnectionIdLength source_connection_id_length, |
| 3938 | bool includes_version, |
| 3939 | bool includes_diversification_nonce, |
| 3940 | QuicPacketNumberLength packet_number_length, |
| 3941 | QuicVariableLengthIntegerLength retry_token_length_length, |
| 3942 | uint64_t retry_token_length, |
| 3943 | QuicVariableLengthIntegerLength length_length) { |
| 3944 | // TODO(ianswett): This is identical to QuicData::AssociatedData. |
| 3945 | return QuicStringPiece( |
| 3946 | encrypted.data(), |
| 3947 | GetStartOfEncryptedData(version, destination_connection_id_length, |
| 3948 | source_connection_id_length, includes_version, |
| 3949 | includes_diversification_nonce, |
| 3950 | packet_number_length, retry_token_length_length, |
| 3951 | retry_token_length, length_length)); |
| 3952 | } |
| 3953 | |
| 3954 | void QuicFramer::SetDecrypter(EncryptionLevel level, |
| 3955 | std::unique_ptr<QuicDecrypter> decrypter) { |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 3956 | DCHECK_EQ(alternative_decrypter_level_, NUM_ENCRYPTION_LEVELS); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3957 | DCHECK_GE(level, decrypter_level_); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 3958 | DCHECK(!version_.KnowsWhichDecrypterToUse()); |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 3959 | decrypter_[decrypter_level_] = nullptr; |
| 3960 | decrypter_[level] = std::move(decrypter); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3961 | decrypter_level_ = level; |
| 3962 | } |
| 3963 | |
| 3964 | void QuicFramer::SetAlternativeDecrypter( |
| 3965 | EncryptionLevel level, |
| 3966 | std::unique_ptr<QuicDecrypter> decrypter, |
| 3967 | bool latch_once_used) { |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 3968 | DCHECK_NE(level, decrypter_level_); |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 3969 | DCHECK(!version_.KnowsWhichDecrypterToUse()); |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 3970 | if (alternative_decrypter_level_ != NUM_ENCRYPTION_LEVELS) { |
| 3971 | decrypter_[alternative_decrypter_level_] = nullptr; |
| 3972 | } |
| 3973 | decrypter_[level] = std::move(decrypter); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3974 | alternative_decrypter_level_ = level; |
| 3975 | alternative_decrypter_latch_ = latch_once_used; |
| 3976 | } |
| 3977 | |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 3978 | void QuicFramer::InstallDecrypter(EncryptionLevel level, |
| 3979 | std::unique_ptr<QuicDecrypter> decrypter) { |
| 3980 | DCHECK(version_.KnowsWhichDecrypterToUse()); |
| 3981 | decrypter_[level] = std::move(decrypter); |
| 3982 | } |
| 3983 | |
| 3984 | void QuicFramer::RemoveDecrypter(EncryptionLevel level) { |
| 3985 | DCHECK(version_.KnowsWhichDecrypterToUse()); |
| 3986 | decrypter_[level] = nullptr; |
| 3987 | } |
| 3988 | |
| 3989 | const QuicDecrypter* QuicFramer::GetDecrypter(EncryptionLevel level) const { |
| 3990 | DCHECK(version_.KnowsWhichDecrypterToUse()); |
| 3991 | return decrypter_[level].get(); |
| 3992 | } |
| 3993 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3994 | const QuicDecrypter* QuicFramer::decrypter() const { |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 3995 | return decrypter_[decrypter_level_].get(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | const QuicDecrypter* QuicFramer::alternative_decrypter() const { |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 3999 | if (alternative_decrypter_level_ == NUM_ENCRYPTION_LEVELS) { |
| 4000 | return nullptr; |
| 4001 | } |
| 4002 | return decrypter_[alternative_decrypter_level_].get(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | void QuicFramer::SetEncrypter(EncryptionLevel level, |
| 4006 | std::unique_ptr<QuicEncrypter> encrypter) { |
| 4007 | DCHECK_GE(level, 0); |
| 4008 | DCHECK_LT(level, NUM_ENCRYPTION_LEVELS); |
| 4009 | encrypter_[level] = std::move(encrypter); |
| 4010 | } |
| 4011 | |
| 4012 | size_t QuicFramer::EncryptInPlace(EncryptionLevel level, |
| 4013 | QuicPacketNumber packet_number, |
| 4014 | size_t ad_len, |
| 4015 | size_t total_len, |
| 4016 | size_t buffer_len, |
| 4017 | char* buffer) { |
| 4018 | DCHECK(packet_number.IsInitialized()); |
dschinazi | 2c5386e | 2019-04-16 16:37:37 -0700 | [diff] [blame] | 4019 | if (encrypter_[level] == nullptr) { |
| 4020 | QUIC_BUG << ENDPOINT |
| 4021 | << "Attempted to encrypt in place without encrypter at level " |
| 4022 | << QuicUtils::EncryptionLevelToString(level); |
| 4023 | RaiseError(QUIC_ENCRYPTION_FAILURE); |
| 4024 | return 0; |
| 4025 | } |
| 4026 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4027 | size_t output_length = 0; |
| 4028 | if (!encrypter_[level]->EncryptPacket( |
| 4029 | packet_number.ToUint64(), |
| 4030 | QuicStringPiece(buffer, ad_len), // Associated data |
| 4031 | QuicStringPiece(buffer + ad_len, total_len - ad_len), // Plaintext |
| 4032 | buffer + ad_len, // Destination buffer |
| 4033 | &output_length, buffer_len - ad_len)) { |
| 4034 | RaiseError(QUIC_ENCRYPTION_FAILURE); |
| 4035 | return 0; |
| 4036 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 4037 | if (version_.HasHeaderProtection() && |
| 4038 | !ApplyHeaderProtection(level, buffer, ad_len + output_length, ad_len)) { |
| 4039 | QUIC_DLOG(ERROR) << "Applying header protection failed."; |
| 4040 | RaiseError(QUIC_ENCRYPTION_FAILURE); |
| 4041 | return 0; |
| 4042 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4043 | |
| 4044 | return ad_len + output_length; |
| 4045 | } |
| 4046 | |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 4047 | namespace { |
| 4048 | |
| 4049 | const size_t kHPSampleLen = 16; |
| 4050 | |
| 4051 | constexpr bool IsLongHeader(uint8_t type_byte) { |
| 4052 | return (type_byte & FLAGS_LONG_HEADER) != 0; |
| 4053 | } |
| 4054 | |
| 4055 | } // namespace |
| 4056 | |
| 4057 | bool QuicFramer::ApplyHeaderProtection(EncryptionLevel level, |
| 4058 | char* buffer, |
| 4059 | size_t buffer_len, |
| 4060 | size_t ad_len) { |
| 4061 | QuicDataReader buffer_reader(buffer, buffer_len); |
| 4062 | QuicDataWriter buffer_writer(buffer_len, buffer); |
| 4063 | // The sample starts 4 bytes after the start of the packet number. |
| 4064 | if (ad_len < last_written_packet_number_length_) { |
| 4065 | return false; |
| 4066 | } |
| 4067 | size_t pn_offset = ad_len - last_written_packet_number_length_; |
| 4068 | // Sample the ciphertext and generate the mask to use for header protection. |
| 4069 | size_t sample_offset = pn_offset + 4; |
| 4070 | QuicDataReader sample_reader(buffer, buffer_len); |
| 4071 | QuicStringPiece sample; |
| 4072 | if (!sample_reader.Seek(sample_offset) || |
| 4073 | !sample_reader.ReadStringPiece(&sample, kHPSampleLen)) { |
| 4074 | QUIC_BUG << "Not enough bytes to sample: sample_offset " << sample_offset |
| 4075 | << ", sample len: " << kHPSampleLen |
| 4076 | << ", buffer len: " << buffer_len; |
| 4077 | return false; |
| 4078 | } |
| 4079 | |
| 4080 | std::string mask = encrypter_[level]->GenerateHeaderProtectionMask(sample); |
| 4081 | if (mask.empty()) { |
| 4082 | QUIC_BUG << "Unable to generate header protection mask."; |
| 4083 | return false; |
| 4084 | } |
| 4085 | QuicDataReader mask_reader(mask.data(), mask.size()); |
| 4086 | |
| 4087 | // Apply the mask to the 4 or 5 least significant bits of the first byte. |
| 4088 | uint8_t bitmask = 0x1f; |
| 4089 | uint8_t type_byte; |
| 4090 | if (!buffer_reader.ReadUInt8(&type_byte)) { |
| 4091 | return false; |
| 4092 | } |
| 4093 | QuicLongHeaderType header_type; |
| 4094 | if (IsLongHeader(type_byte)) { |
| 4095 | bitmask = 0x0f; |
| 4096 | if (!GetLongHeaderType(version_.transport_version, type_byte, |
| 4097 | &header_type)) { |
| 4098 | return false; |
| 4099 | } |
| 4100 | } |
| 4101 | uint8_t mask_byte; |
| 4102 | if (!mask_reader.ReadUInt8(&mask_byte) || |
| 4103 | !buffer_writer.WriteUInt8(type_byte ^ (mask_byte & bitmask))) { |
| 4104 | return false; |
| 4105 | } |
| 4106 | |
| 4107 | // Adjust |pn_offset| to account for the diversification nonce. |
| 4108 | if (IsLongHeader(type_byte) && header_type == ZERO_RTT_PROTECTED && |
| 4109 | perspective_ == Perspective::IS_SERVER && |
| 4110 | version_.handshake_protocol == PROTOCOL_QUIC_CRYPTO) { |
| 4111 | if (pn_offset <= kDiversificationNonceSize) { |
| 4112 | QUIC_BUG << "Expected diversification nonce, but not enough bytes"; |
| 4113 | return false; |
| 4114 | } |
| 4115 | pn_offset -= kDiversificationNonceSize; |
| 4116 | } |
| 4117 | // Advance the reader and writer to the packet number. Both the reader and |
| 4118 | // writer have each read/written one byte. |
| 4119 | if (!buffer_writer.Seek(pn_offset - 1) || |
| 4120 | !buffer_reader.Seek(pn_offset - 1)) { |
| 4121 | return false; |
| 4122 | } |
| 4123 | // Apply the rest of the mask to the packet number. |
| 4124 | for (size_t i = 0; i < last_written_packet_number_length_; ++i) { |
| 4125 | uint8_t buffer_byte; |
| 4126 | uint8_t mask_byte; |
| 4127 | if (!mask_reader.ReadUInt8(&mask_byte) || |
| 4128 | !buffer_reader.ReadUInt8(&buffer_byte) || |
| 4129 | !buffer_writer.WriteUInt8(buffer_byte ^ mask_byte)) { |
| 4130 | return false; |
| 4131 | } |
| 4132 | } |
| 4133 | return true; |
| 4134 | } |
| 4135 | |
| 4136 | bool QuicFramer::RemoveHeaderProtection(QuicDataReader* reader, |
| 4137 | const QuicEncryptedPacket& packet, |
| 4138 | QuicPacketHeader* header, |
| 4139 | uint64_t* full_packet_number, |
| 4140 | std::vector<char>* associated_data) { |
| 4141 | EncryptionLevel expected_decryption_level = GetEncryptionLevel(*header); |
| 4142 | QuicDecrypter* decrypter = decrypter_[expected_decryption_level].get(); |
| 4143 | if (decrypter == nullptr) { |
| 4144 | QUIC_DVLOG(1) |
| 4145 | << "No decrypter available for removing header protection at level " |
| 4146 | << expected_decryption_level; |
| 4147 | return false; |
| 4148 | } |
| 4149 | |
| 4150 | bool has_diversification_nonce = |
| 4151 | header->form == IETF_QUIC_LONG_HEADER_PACKET && |
| 4152 | header->long_packet_type == ZERO_RTT_PROTECTED && |
| 4153 | perspective_ == Perspective::IS_CLIENT && |
| 4154 | version_.handshake_protocol == PROTOCOL_QUIC_CRYPTO; |
| 4155 | |
| 4156 | // Read a sample from the ciphertext and compute the mask to use for header |
| 4157 | // protection. |
| 4158 | QuicStringPiece remaining_packet = reader->PeekRemainingPayload(); |
| 4159 | QuicDataReader sample_reader(remaining_packet); |
| 4160 | |
| 4161 | // The sample starts 4 bytes after the start of the packet number. |
| 4162 | QuicStringPiece pn; |
| 4163 | if (!sample_reader.ReadStringPiece(&pn, 4)) { |
| 4164 | QUIC_DVLOG(1) << "Not enough data to sample"; |
| 4165 | return false; |
| 4166 | } |
| 4167 | if (has_diversification_nonce) { |
| 4168 | // In Google QUIC, the diversification nonce comes between the packet number |
| 4169 | // and the sample. |
| 4170 | if (!sample_reader.Seek(kDiversificationNonceSize)) { |
| 4171 | QUIC_DVLOG(1) << "No diversification nonce to skip over"; |
| 4172 | return false; |
| 4173 | } |
| 4174 | } |
| 4175 | std::string mask = decrypter->GenerateHeaderProtectionMask(&sample_reader); |
| 4176 | QuicDataReader mask_reader(mask.data(), mask.size()); |
| 4177 | if (mask.empty()) { |
| 4178 | QUIC_DVLOG(1) << "Failed to compute mask"; |
| 4179 | return false; |
| 4180 | } |
| 4181 | |
| 4182 | // Unmask the rest of the type byte. |
| 4183 | uint8_t bitmask = 0x1f; |
| 4184 | if (IsLongHeader(header->type_byte)) { |
| 4185 | bitmask = 0x0f; |
| 4186 | } |
| 4187 | uint8_t mask_byte; |
| 4188 | if (!mask_reader.ReadUInt8(&mask_byte)) { |
| 4189 | QUIC_DVLOG(1) << "No first byte to read from mask"; |
| 4190 | return false; |
| 4191 | } |
| 4192 | header->type_byte ^= (mask_byte & bitmask); |
| 4193 | |
| 4194 | // Compute the packet number length. |
| 4195 | header->packet_number_length = |
| 4196 | static_cast<QuicPacketNumberLength>((header->type_byte & 0x03) + 1); |
| 4197 | |
| 4198 | char pn_buffer[IETF_MAX_PACKET_NUMBER_LENGTH] = {}; |
| 4199 | QuicDataWriter pn_writer(QUIC_ARRAYSIZE(pn_buffer), pn_buffer); |
| 4200 | |
| 4201 | // Read the (protected) packet number from the reader and unmask the packet |
| 4202 | // number. |
| 4203 | for (size_t i = 0; i < header->packet_number_length; ++i) { |
| 4204 | uint8_t protected_pn_byte, mask_byte; |
| 4205 | if (!mask_reader.ReadUInt8(&mask_byte) || |
| 4206 | !reader->ReadUInt8(&protected_pn_byte) || |
| 4207 | !pn_writer.WriteUInt8(protected_pn_byte ^ mask_byte)) { |
| 4208 | QUIC_DVLOG(1) << "Failed to unmask packet number"; |
| 4209 | return false; |
| 4210 | } |
| 4211 | } |
| 4212 | QuicDataReader packet_number_reader(pn_writer.data(), pn_writer.length()); |
| 4213 | QuicPacketNumber base_packet_number; |
| 4214 | if (supports_multiple_packet_number_spaces_) { |
| 4215 | PacketNumberSpace pn_space = GetPacketNumberSpace(*header); |
| 4216 | if (pn_space == NUM_PACKET_NUMBER_SPACES) { |
| 4217 | return false; |
| 4218 | } |
| 4219 | base_packet_number = largest_decrypted_packet_numbers_[pn_space]; |
| 4220 | } else { |
| 4221 | base_packet_number = largest_packet_number_; |
| 4222 | } |
| 4223 | if (!ProcessAndCalculatePacketNumber( |
| 4224 | &packet_number_reader, header->packet_number_length, |
| 4225 | base_packet_number, full_packet_number)) { |
| 4226 | return false; |
| 4227 | } |
| 4228 | |
| 4229 | // Get the associated data, and apply the same unmasking operations to it. |
| 4230 | QuicStringPiece ad = GetAssociatedDataFromEncryptedPacket( |
| 4231 | version_.transport_version, packet, |
| 4232 | GetIncludedDestinationConnectionIdLength(*header), |
| 4233 | GetIncludedSourceConnectionIdLength(*header), header->version_flag, |
| 4234 | has_diversification_nonce, header->packet_number_length, |
| 4235 | header->retry_token_length_length, header->retry_token.length(), |
| 4236 | header->length_length); |
| 4237 | *associated_data = std::vector<char>(ad.begin(), ad.end()); |
| 4238 | QuicDataWriter ad_writer(associated_data->size(), associated_data->data()); |
| 4239 | |
| 4240 | // Apply the unmasked type byte and packet number to |associated_data|. |
| 4241 | if (!ad_writer.WriteUInt8(header->type_byte)) { |
| 4242 | return false; |
| 4243 | } |
| 4244 | // Put the packet number at the end of the AD, or if there's a diversification |
| 4245 | // nonce, before that (which is at the end of the AD). |
| 4246 | size_t seek_len = ad_writer.remaining() - header->packet_number_length; |
| 4247 | if (has_diversification_nonce) { |
| 4248 | seek_len -= kDiversificationNonceSize; |
| 4249 | } |
| 4250 | if (!ad_writer.Seek(seek_len) || |
| 4251 | !ad_writer.WriteBytes(pn_writer.data(), pn_writer.length())) { |
| 4252 | QUIC_DVLOG(1) << "Failed to apply unmasking operations to AD"; |
| 4253 | return false; |
| 4254 | } |
| 4255 | |
| 4256 | return true; |
| 4257 | } |
| 4258 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4259 | size_t QuicFramer::EncryptPayload(EncryptionLevel level, |
| 4260 | QuicPacketNumber packet_number, |
| 4261 | const QuicPacket& packet, |
| 4262 | char* buffer, |
| 4263 | size_t buffer_len) { |
| 4264 | DCHECK(packet_number.IsInitialized()); |
dschinazi | 2c5386e | 2019-04-16 16:37:37 -0700 | [diff] [blame] | 4265 | if (encrypter_[level] == nullptr) { |
| 4266 | QUIC_BUG << ENDPOINT << "Attempted to encrypt without encrypter at level " |
| 4267 | << QuicUtils::EncryptionLevelToString(level); |
| 4268 | RaiseError(QUIC_ENCRYPTION_FAILURE); |
| 4269 | return 0; |
| 4270 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4271 | |
| 4272 | QuicStringPiece associated_data = |
| 4273 | packet.AssociatedData(version_.transport_version); |
| 4274 | // Copy in the header, because the encrypter only populates the encrypted |
| 4275 | // plaintext content. |
| 4276 | const size_t ad_len = associated_data.length(); |
| 4277 | memmove(buffer, associated_data.data(), ad_len); |
| 4278 | // Encrypt the plaintext into the buffer. |
| 4279 | size_t output_length = 0; |
| 4280 | if (!encrypter_[level]->EncryptPacket( |
| 4281 | packet_number.ToUint64(), associated_data, |
| 4282 | packet.Plaintext(version_.transport_version), buffer + ad_len, |
| 4283 | &output_length, buffer_len - ad_len)) { |
| 4284 | RaiseError(QUIC_ENCRYPTION_FAILURE); |
| 4285 | return 0; |
| 4286 | } |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 4287 | if (version_.HasHeaderProtection() && |
| 4288 | !ApplyHeaderProtection(level, buffer, ad_len + output_length, ad_len)) { |
| 4289 | QUIC_DLOG(ERROR) << "Applying header protection failed."; |
| 4290 | RaiseError(QUIC_ENCRYPTION_FAILURE); |
| 4291 | return 0; |
| 4292 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4293 | |
| 4294 | return ad_len + output_length; |
| 4295 | } |
| 4296 | |
| 4297 | size_t QuicFramer::GetCiphertextSize(EncryptionLevel level, |
| 4298 | size_t plaintext_size) const { |
| 4299 | return encrypter_[level]->GetCiphertextSize(plaintext_size); |
| 4300 | } |
| 4301 | |
| 4302 | size_t QuicFramer::GetMaxPlaintextSize(size_t ciphertext_size) { |
| 4303 | // In order to keep the code simple, we don't have the current encryption |
| 4304 | // level to hand. Both the NullEncrypter and AES-GCM have a tag length of 12. |
| 4305 | size_t min_plaintext_size = ciphertext_size; |
| 4306 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 4307 | for (int i = ENCRYPTION_INITIAL; i < NUM_ENCRYPTION_LEVELS; i++) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4308 | if (encrypter_[i] != nullptr) { |
| 4309 | size_t size = encrypter_[i]->GetMaxPlaintextSize(ciphertext_size); |
| 4310 | if (size < min_plaintext_size) { |
| 4311 | min_plaintext_size = size; |
| 4312 | } |
| 4313 | } |
| 4314 | } |
| 4315 | |
| 4316 | return min_plaintext_size; |
| 4317 | } |
| 4318 | |
| 4319 | bool QuicFramer::DecryptPayload(QuicStringPiece encrypted, |
| 4320 | QuicStringPiece associated_data, |
| 4321 | const QuicPacketHeader& header, |
| 4322 | char* decrypted_buffer, |
| 4323 | size_t buffer_length, |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 4324 | size_t* decrypted_length, |
| 4325 | EncryptionLevel* decrypted_level) { |
nharper | 855d217 | 2019-05-02 16:17:46 -0700 | [diff] [blame] | 4326 | if (!EncryptionLevelIsValid(decrypter_level_)) { |
| 4327 | QUIC_BUG << "Attempted to decrypt with bad decrypter_level_"; |
| 4328 | return false; |
| 4329 | } |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4330 | EncryptionLevel level = decrypter_level_; |
| 4331 | QuicDecrypter* decrypter = decrypter_[level].get(); |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 4332 | QuicDecrypter* alternative_decrypter = nullptr; |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4333 | if (version().KnowsWhichDecrypterToUse()) { |
nharper | 9bb8346 | 2019-05-01 10:53:22 -0700 | [diff] [blame] | 4334 | QUIC_RELOADABLE_FLAG_COUNT(quic_v44_disable_trial_decryption); |
nharper | 855d217 | 2019-05-02 16:17:46 -0700 | [diff] [blame] | 4335 | if (header.form == GOOGLE_QUIC_PACKET) { |
| 4336 | QUIC_BUG << "Attempted to decrypt GOOGLE_QUIC_PACKET with a version that " |
| 4337 | "knows which decrypter to use"; |
| 4338 | return false; |
| 4339 | } |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4340 | level = GetEncryptionLevel(header); |
nharper | 855d217 | 2019-05-02 16:17:46 -0700 | [diff] [blame] | 4341 | if (!EncryptionLevelIsValid(level)) { |
| 4342 | QUIC_BUG << "Attempted to decrypt with bad level"; |
| 4343 | return false; |
| 4344 | } |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4345 | decrypter = decrypter_[level].get(); |
| 4346 | if (decrypter == nullptr) { |
| 4347 | return false; |
| 4348 | } |
| 4349 | if (level == ENCRYPTION_ZERO_RTT && |
| 4350 | perspective_ == Perspective::IS_CLIENT && header.nonce != nullptr) { |
| 4351 | decrypter->SetDiversificationNonce(*header.nonce); |
| 4352 | } |
| 4353 | } else if (alternative_decrypter_level_ != NUM_ENCRYPTION_LEVELS) { |
nharper | 855d217 | 2019-05-02 16:17:46 -0700 | [diff] [blame] | 4354 | if (!EncryptionLevelIsValid(alternative_decrypter_level_)) { |
| 4355 | QUIC_BUG << "Attempted to decrypt with bad alternative_decrypter_level_"; |
| 4356 | return false; |
| 4357 | } |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 4358 | alternative_decrypter = decrypter_[alternative_decrypter_level_].get(); |
| 4359 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4360 | |
nharper | 855d217 | 2019-05-02 16:17:46 -0700 | [diff] [blame] | 4361 | if (decrypter == nullptr) { |
| 4362 | QUIC_BUG << "Attempting to decrypt without decrypter"; |
| 4363 | return false; |
| 4364 | } |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4365 | |
| 4366 | bool success = decrypter->DecryptPacket( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4367 | header.packet_number.ToUint64(), associated_data, encrypted, |
| 4368 | decrypted_buffer, decrypted_length, buffer_length); |
| 4369 | if (success) { |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 4370 | visitor_->OnDecryptedPacket(level); |
| 4371 | *decrypted_level = level; |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 4372 | } else if (alternative_decrypter != nullptr) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4373 | if (header.nonce != nullptr) { |
| 4374 | DCHECK_EQ(perspective_, Perspective::IS_CLIENT); |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 4375 | alternative_decrypter->SetDiversificationNonce(*header.nonce); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4376 | } |
| 4377 | bool try_alternative_decryption = true; |
| 4378 | if (alternative_decrypter_level_ == ENCRYPTION_ZERO_RTT) { |
| 4379 | if (perspective_ == Perspective::IS_CLIENT) { |
| 4380 | if (header.nonce == nullptr) { |
| 4381 | // Can not use INITIAL decryption without a diversification nonce. |
| 4382 | try_alternative_decryption = false; |
| 4383 | } |
| 4384 | } else { |
| 4385 | DCHECK(header.nonce == nullptr); |
| 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | if (try_alternative_decryption) { |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 4390 | success = alternative_decrypter->DecryptPacket( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4391 | header.packet_number.ToUint64(), associated_data, encrypted, |
| 4392 | decrypted_buffer, decrypted_length, buffer_length); |
| 4393 | } |
| 4394 | if (success) { |
| 4395 | visitor_->OnDecryptedPacket(alternative_decrypter_level_); |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 4396 | *decrypted_level = decrypter_level_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4397 | if (alternative_decrypter_latch_) { |
nharper | 855d217 | 2019-05-02 16:17:46 -0700 | [diff] [blame] | 4398 | if (!EncryptionLevelIsValid(alternative_decrypter_level_)) { |
| 4399 | QUIC_BUG << "Attempted to latch alternate decrypter with bad " |
| 4400 | "alternative_decrypter_level_"; |
| 4401 | return false; |
| 4402 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4403 | // Switch to the alternative decrypter and latch so that we cannot |
| 4404 | // switch back. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4405 | decrypter_level_ = alternative_decrypter_level_; |
QUICHE team | 76086e4 | 2019-03-25 15:12:29 -0700 | [diff] [blame] | 4406 | alternative_decrypter_level_ = NUM_ENCRYPTION_LEVELS; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4407 | } else { |
| 4408 | // Switch the alternative decrypter so that we use it first next time. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4409 | EncryptionLevel level = alternative_decrypter_level_; |
| 4410 | alternative_decrypter_level_ = decrypter_level_; |
| 4411 | decrypter_level_ = level; |
| 4412 | } |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | if (!success) { |
| 4417 | QUIC_DVLOG(1) << ENDPOINT << "DecryptPacket failed for packet_number:" |
| 4418 | << header.packet_number; |
| 4419 | return false; |
| 4420 | } |
| 4421 | |
| 4422 | return true; |
| 4423 | } |
| 4424 | |
| 4425 | size_t QuicFramer::GetIetfAckFrameSize(const QuicAckFrame& frame) { |
| 4426 | // Type byte, largest_acked, and delay_time are straight-forward. |
| 4427 | size_t ack_frame_size = kQuicFrameTypeSize; |
| 4428 | QuicPacketNumber largest_acked = LargestAcked(frame); |
| 4429 | ack_frame_size += QuicDataWriter::GetVarInt62Len(largest_acked.ToUint64()); |
| 4430 | uint64_t ack_delay_time_us; |
| 4431 | ack_delay_time_us = frame.ack_delay_time.ToMicroseconds(); |
| 4432 | ack_delay_time_us = ack_delay_time_us >> kIetfAckTimestampShift; |
| 4433 | ack_frame_size += QuicDataWriter::GetVarInt62Len(ack_delay_time_us); |
| 4434 | |
| 4435 | // If |ecn_counters_populated| is true and any of the ecn counters is non-0 |
| 4436 | // then the ecn counters are included... |
| 4437 | if (frame.ecn_counters_populated && |
| 4438 | (frame.ect_0_count || frame.ect_1_count || frame.ecn_ce_count)) { |
| 4439 | ack_frame_size += QuicDataWriter::GetVarInt62Len(frame.ect_0_count); |
| 4440 | ack_frame_size += QuicDataWriter::GetVarInt62Len(frame.ect_1_count); |
| 4441 | ack_frame_size += QuicDataWriter::GetVarInt62Len(frame.ecn_ce_count); |
| 4442 | } |
| 4443 | |
| 4444 | // The rest (ack_block_count, first_ack_block, and additional ack |
| 4445 | // blocks, if any) depends: |
| 4446 | uint64_t ack_block_count = frame.packets.NumIntervals(); |
| 4447 | if (ack_block_count == 0) { |
| 4448 | // If the QuicAckFrame has no Intervals, then it is interpreted |
| 4449 | // as an ack of a single packet at QuicAckFrame.largest_acked. |
| 4450 | // The resulting ack will consist of only the frame's |
| 4451 | // largest_ack & first_ack_block fields. The first ack block will be 0 |
| 4452 | // (indicating a single packet) and the ack block_count will be 0. |
| 4453 | // Each 0 takes 1 byte when VarInt62 encoded. |
| 4454 | ack_frame_size += 2; |
| 4455 | return ack_frame_size; |
| 4456 | } |
| 4457 | |
| 4458 | auto itr = frame.packets.rbegin(); |
| 4459 | QuicPacketNumber ack_block_largest = largest_acked; |
| 4460 | QuicPacketNumber ack_block_smallest; |
| 4461 | if ((itr->max() - 1) == largest_acked) { |
| 4462 | // If largest_acked + 1 is equal to the Max() of the first Interval |
| 4463 | // in the QuicAckFrame then the first Interval is the first ack block of the |
| 4464 | // frame; remaining Intervals are additional ack blocks. The QuicAckFrame's |
| 4465 | // first Interval is encoded in the frame's largest_acked/first_ack_block, |
| 4466 | // the remaining Intervals are encoded in additional ack blocks in the |
| 4467 | // frame, and the packet's ack_block_count is the number of QuicAckFrame |
| 4468 | // Intervals - 1. |
| 4469 | ack_block_smallest = itr->min(); |
| 4470 | itr++; |
| 4471 | ack_block_count--; |
| 4472 | } else { |
| 4473 | // If QuicAckFrame.largest_acked is NOT equal to the Max() of |
| 4474 | // the first Interval then it is interpreted as acking a single |
| 4475 | // packet at QuicAckFrame.largest_acked, with additional |
| 4476 | // Intervals indicating additional ack blocks. The encoding is |
| 4477 | // a) The packet's largest_acked is the QuicAckFrame's largest |
| 4478 | // acked, |
| 4479 | // b) the first ack block size is 0, |
| 4480 | // c) The packet's ack_block_count is the number of QuicAckFrame |
| 4481 | // Intervals, and |
| 4482 | // d) The QuicAckFrame Intervals are encoded in additional ack |
| 4483 | // blocks in the packet. |
| 4484 | ack_block_smallest = largest_acked; |
| 4485 | } |
| 4486 | size_t ack_block_count_size = QuicDataWriter::GetVarInt62Len(ack_block_count); |
| 4487 | ack_frame_size += ack_block_count_size; |
| 4488 | |
| 4489 | uint64_t first_ack_block = ack_block_largest - ack_block_smallest; |
| 4490 | size_t first_ack_block_size = QuicDataWriter::GetVarInt62Len(first_ack_block); |
| 4491 | ack_frame_size += first_ack_block_size; |
| 4492 | |
| 4493 | // Account for the remaining Intervals, if any. |
| 4494 | while (ack_block_count != 0) { |
| 4495 | uint64_t gap_size = ack_block_smallest - itr->max(); |
| 4496 | // Decrement per the protocol specification |
| 4497 | size_t size_of_gap_size = QuicDataWriter::GetVarInt62Len(gap_size - 1); |
| 4498 | ack_frame_size += size_of_gap_size; |
| 4499 | |
| 4500 | uint64_t block_size = itr->max() - itr->min(); |
| 4501 | // Decrement per the protocol specification |
| 4502 | size_t size_of_block_size = QuicDataWriter::GetVarInt62Len(block_size - 1); |
| 4503 | ack_frame_size += size_of_block_size; |
| 4504 | |
| 4505 | ack_block_smallest = itr->min(); |
| 4506 | itr++; |
| 4507 | ack_block_count--; |
| 4508 | } |
| 4509 | |
| 4510 | return ack_frame_size; |
| 4511 | } |
| 4512 | |
| 4513 | size_t QuicFramer::GetAckFrameSize( |
| 4514 | const QuicAckFrame& ack, |
| 4515 | QuicPacketNumberLength packet_number_length) { |
| 4516 | DCHECK(!ack.packets.Empty()); |
| 4517 | size_t ack_size = 0; |
| 4518 | |
| 4519 | if (version_.transport_version == QUIC_VERSION_99) { |
| 4520 | return GetIetfAckFrameSize(ack); |
| 4521 | } |
| 4522 | AckFrameInfo ack_info = GetAckFrameInfo(ack); |
| 4523 | QuicPacketNumberLength largest_acked_length = |
| 4524 | GetMinPacketNumberLength(version_.transport_version, LargestAcked(ack)); |
| 4525 | QuicPacketNumberLength ack_block_length = GetMinPacketNumberLength( |
| 4526 | version_.transport_version, QuicPacketNumber(ack_info.max_block_length)); |
| 4527 | |
| 4528 | ack_size = |
| 4529 | GetMinAckFrameSize(version_.transport_version, largest_acked_length); |
| 4530 | // First ack block length. |
| 4531 | ack_size += ack_block_length; |
| 4532 | if (ack_info.num_ack_blocks != 0) { |
| 4533 | ack_size += kNumberOfAckBlocksSize; |
| 4534 | ack_size += std::min(ack_info.num_ack_blocks, kMaxAckBlocks) * |
| 4535 | (ack_block_length + PACKET_1BYTE_PACKET_NUMBER); |
| 4536 | } |
| 4537 | |
| 4538 | // Include timestamps. |
| 4539 | if (process_timestamps_) { |
| 4540 | ack_size += GetAckFrameTimeStampSize(ack); |
| 4541 | } |
| 4542 | |
| 4543 | return ack_size; |
| 4544 | } |
| 4545 | |
| 4546 | size_t QuicFramer::GetAckFrameTimeStampSize(const QuicAckFrame& ack) { |
| 4547 | if (ack.received_packet_times.empty()) { |
| 4548 | return 0; |
| 4549 | } |
| 4550 | |
| 4551 | return kQuicNumTimestampsLength + kQuicFirstTimestampLength + |
| 4552 | (kQuicTimestampLength + kQuicTimestampPacketNumberGapLength) * |
| 4553 | (ack.received_packet_times.size() - 1); |
| 4554 | } |
| 4555 | |
| 4556 | size_t QuicFramer::ComputeFrameLength( |
| 4557 | const QuicFrame& frame, |
| 4558 | bool last_frame_in_packet, |
| 4559 | QuicPacketNumberLength packet_number_length) { |
| 4560 | switch (frame.type) { |
| 4561 | case STREAM_FRAME: |
| 4562 | return GetMinStreamFrameSize( |
| 4563 | version_.transport_version, frame.stream_frame.stream_id, |
| 4564 | frame.stream_frame.offset, last_frame_in_packet, |
| 4565 | frame.stream_frame.data_length) + |
| 4566 | frame.stream_frame.data_length; |
| 4567 | case CRYPTO_FRAME: |
| 4568 | return GetMinCryptoFrameSize(frame.crypto_frame->offset, |
| 4569 | frame.crypto_frame->data_length) + |
| 4570 | frame.crypto_frame->data_length; |
| 4571 | case ACK_FRAME: { |
| 4572 | return GetAckFrameSize(*frame.ack_frame, packet_number_length); |
| 4573 | } |
| 4574 | case STOP_WAITING_FRAME: |
| 4575 | return GetStopWaitingFrameSize(version_.transport_version, |
| 4576 | packet_number_length); |
| 4577 | case MTU_DISCOVERY_FRAME: |
| 4578 | // MTU discovery frames are serialized as ping frames. |
| 4579 | return kQuicFrameTypeSize; |
| 4580 | case MESSAGE_FRAME: |
| 4581 | return GetMessageFrameSize(version_.transport_version, |
| 4582 | last_frame_in_packet, |
| 4583 | frame.message_frame->message_length); |
| 4584 | case PADDING_FRAME: |
| 4585 | DCHECK(false); |
| 4586 | return 0; |
| 4587 | default: |
| 4588 | return GetRetransmittableControlFrameSize(version_.transport_version, |
| 4589 | frame); |
| 4590 | } |
| 4591 | } |
| 4592 | |
| 4593 | bool QuicFramer::AppendTypeByte(const QuicFrame& frame, |
| 4594 | bool last_frame_in_packet, |
| 4595 | QuicDataWriter* writer) { |
| 4596 | if (version_.transport_version == QUIC_VERSION_99) { |
| 4597 | return AppendIetfTypeByte(frame, last_frame_in_packet, writer); |
| 4598 | } |
| 4599 | uint8_t type_byte = 0; |
| 4600 | switch (frame.type) { |
| 4601 | case STREAM_FRAME: |
| 4602 | type_byte = |
| 4603 | GetStreamFrameTypeByte(frame.stream_frame, last_frame_in_packet); |
| 4604 | break; |
| 4605 | case ACK_FRAME: |
| 4606 | return true; |
| 4607 | case MTU_DISCOVERY_FRAME: |
| 4608 | type_byte = static_cast<uint8_t>(PING_FRAME); |
| 4609 | break; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4610 | case NEW_CONNECTION_ID_FRAME: |
| 4611 | set_detailed_error( |
| 4612 | "Attempt to append NEW_CONNECTION_ID frame and not in version 99."); |
| 4613 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4614 | case RETIRE_CONNECTION_ID_FRAME: |
| 4615 | set_detailed_error( |
| 4616 | "Attempt to append RETIRE_CONNECTION_ID frame and not in version " |
| 4617 | "99."); |
| 4618 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4619 | case NEW_TOKEN_FRAME: |
| 4620 | set_detailed_error( |
| 4621 | "Attempt to append NEW_TOKEN frame and not in version 99."); |
| 4622 | return RaiseError(QUIC_INTERNAL_ERROR); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4623 | case MAX_STREAMS_FRAME: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4624 | set_detailed_error( |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4625 | "Attempt to append MAX_STREAMS frame and not in version 99."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4626 | return RaiseError(QUIC_INTERNAL_ERROR); |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4627 | case STREAMS_BLOCKED_FRAME: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4628 | set_detailed_error( |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4629 | "Attempt to append STREAMS_BLOCKED frame and not in version 99."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4630 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4631 | case PATH_RESPONSE_FRAME: |
| 4632 | set_detailed_error( |
| 4633 | "Attempt to append PATH_RESPONSE frame and not in version 99."); |
| 4634 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4635 | case PATH_CHALLENGE_FRAME: |
| 4636 | set_detailed_error( |
| 4637 | "Attempt to append PATH_CHALLENGE frame and not in version 99."); |
| 4638 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4639 | case STOP_SENDING_FRAME: |
| 4640 | set_detailed_error( |
| 4641 | "Attempt to append STOP_SENDING frame and not in version 99."); |
| 4642 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4643 | case MESSAGE_FRAME: |
| 4644 | return true; |
| 4645 | |
| 4646 | default: |
| 4647 | type_byte = static_cast<uint8_t>(frame.type); |
| 4648 | break; |
| 4649 | } |
| 4650 | |
| 4651 | return writer->WriteUInt8(type_byte); |
| 4652 | } |
| 4653 | |
| 4654 | bool QuicFramer::AppendIetfTypeByte(const QuicFrame& frame, |
| 4655 | bool last_frame_in_packet, |
| 4656 | QuicDataWriter* writer) { |
| 4657 | uint8_t type_byte = 0; |
| 4658 | switch (frame.type) { |
| 4659 | case PADDING_FRAME: |
| 4660 | type_byte = IETF_PADDING; |
| 4661 | break; |
| 4662 | case RST_STREAM_FRAME: |
| 4663 | type_byte = IETF_RST_STREAM; |
| 4664 | break; |
| 4665 | case CONNECTION_CLOSE_FRAME: |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 4666 | switch (frame.connection_close_frame->close_type) { |
| 4667 | case IETF_QUIC_APPLICATION_CONNECTION_CLOSE: |
| 4668 | type_byte = IETF_APPLICATION_CLOSE; |
| 4669 | break; |
| 4670 | case IETF_QUIC_TRANSPORT_CONNECTION_CLOSE: |
| 4671 | type_byte = IETF_CONNECTION_CLOSE; |
| 4672 | break; |
| 4673 | default: |
| 4674 | set_detailed_error("Invalid QuicConnectionCloseFrame type."); |
| 4675 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4676 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4677 | break; |
| 4678 | case GOAWAY_FRAME: |
| 4679 | set_detailed_error( |
| 4680 | "Attempt to create non-version-99 GOAWAY frame in version 99."); |
| 4681 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4682 | case WINDOW_UPDATE_FRAME: |
| 4683 | // Depending on whether there is a stream ID or not, will be either a |
| 4684 | // MAX_STREAM_DATA frame or a MAX_DATA frame. |
| 4685 | if (frame.window_update_frame->stream_id == |
| 4686 | QuicUtils::GetInvalidStreamId(transport_version())) { |
| 4687 | type_byte = IETF_MAX_DATA; |
| 4688 | } else { |
| 4689 | type_byte = IETF_MAX_STREAM_DATA; |
| 4690 | } |
| 4691 | break; |
| 4692 | case BLOCKED_FRAME: |
| 4693 | if (frame.blocked_frame->stream_id == |
| 4694 | QuicUtils::GetInvalidStreamId(transport_version())) { |
| 4695 | type_byte = IETF_BLOCKED; |
| 4696 | } else { |
| 4697 | type_byte = IETF_STREAM_BLOCKED; |
| 4698 | } |
| 4699 | break; |
| 4700 | case STOP_WAITING_FRAME: |
| 4701 | set_detailed_error( |
| 4702 | "Attempt to append type byte of STOP WAITING frame in version 99."); |
| 4703 | return RaiseError(QUIC_INTERNAL_ERROR); |
| 4704 | case PING_FRAME: |
| 4705 | type_byte = IETF_PING; |
| 4706 | break; |
| 4707 | case STREAM_FRAME: |
| 4708 | type_byte = |
| 4709 | GetStreamFrameTypeByte(frame.stream_frame, last_frame_in_packet); |
| 4710 | break; |
| 4711 | case ACK_FRAME: |
| 4712 | // Do nothing here, AppendIetfAckFrameAndTypeByte() will put the type byte |
| 4713 | // in the buffer. |
| 4714 | return true; |
| 4715 | case MTU_DISCOVERY_FRAME: |
| 4716 | // The path MTU discovery frame is encoded as a PING frame on the wire. |
| 4717 | type_byte = IETF_PING; |
| 4718 | break; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4719 | case NEW_CONNECTION_ID_FRAME: |
| 4720 | type_byte = IETF_NEW_CONNECTION_ID; |
| 4721 | break; |
| 4722 | case RETIRE_CONNECTION_ID_FRAME: |
| 4723 | type_byte = IETF_RETIRE_CONNECTION_ID; |
| 4724 | break; |
| 4725 | case NEW_TOKEN_FRAME: |
| 4726 | type_byte = IETF_NEW_TOKEN; |
| 4727 | break; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4728 | case MAX_STREAMS_FRAME: |
| 4729 | if (frame.max_streams_frame.unidirectional) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4730 | type_byte = IETF_MAX_STREAMS_UNIDIRECTIONAL; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4731 | } else { |
| 4732 | type_byte = IETF_MAX_STREAMS_BIDIRECTIONAL; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4733 | } |
| 4734 | break; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4735 | case STREAMS_BLOCKED_FRAME: |
| 4736 | if (frame.streams_blocked_frame.unidirectional) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4737 | type_byte = IETF_STREAMS_BLOCKED_UNIDIRECTIONAL; |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 4738 | } else { |
| 4739 | type_byte = IETF_STREAMS_BLOCKED_BIDIRECTIONAL; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4740 | } |
| 4741 | break; |
| 4742 | case PATH_RESPONSE_FRAME: |
| 4743 | type_byte = IETF_PATH_RESPONSE; |
| 4744 | break; |
| 4745 | case PATH_CHALLENGE_FRAME: |
| 4746 | type_byte = IETF_PATH_CHALLENGE; |
| 4747 | break; |
| 4748 | case STOP_SENDING_FRAME: |
| 4749 | type_byte = IETF_STOP_SENDING; |
| 4750 | break; |
| 4751 | case MESSAGE_FRAME: |
| 4752 | return true; |
| 4753 | case CRYPTO_FRAME: |
| 4754 | type_byte = IETF_CRYPTO; |
| 4755 | break; |
| 4756 | default: |
| 4757 | QUIC_BUG << "Attempt to generate a frame type for an unsupported value: " |
| 4758 | << frame.type; |
| 4759 | return false; |
| 4760 | } |
| 4761 | return writer->WriteUInt8(type_byte); |
| 4762 | } |
| 4763 | |
| 4764 | // static |
| 4765 | bool QuicFramer::AppendPacketNumber(QuicPacketNumberLength packet_number_length, |
| 4766 | QuicPacketNumber packet_number, |
| 4767 | QuicDataWriter* writer) { |
| 4768 | DCHECK(packet_number.IsInitialized()); |
| 4769 | if (!IsValidPacketNumberLength(packet_number_length)) { |
| 4770 | QUIC_BUG << "Invalid packet_number_length: " << packet_number_length; |
| 4771 | return false; |
| 4772 | } |
| 4773 | return writer->WriteBytesToUInt64(packet_number_length, |
| 4774 | packet_number.ToUint64()); |
| 4775 | } |
| 4776 | |
| 4777 | // static |
| 4778 | bool QuicFramer::AppendStreamId(size_t stream_id_length, |
| 4779 | QuicStreamId stream_id, |
| 4780 | QuicDataWriter* writer) { |
| 4781 | if (stream_id_length == 0 || stream_id_length > 4) { |
| 4782 | QUIC_BUG << "Invalid stream_id_length: " << stream_id_length; |
| 4783 | return false; |
| 4784 | } |
| 4785 | return writer->WriteBytesToUInt64(stream_id_length, stream_id); |
| 4786 | } |
| 4787 | |
| 4788 | // static |
| 4789 | bool QuicFramer::AppendStreamOffset(size_t offset_length, |
| 4790 | QuicStreamOffset offset, |
| 4791 | QuicDataWriter* writer) { |
| 4792 | if (offset_length == 1 || offset_length > 8) { |
| 4793 | QUIC_BUG << "Invalid stream_offset_length: " << offset_length; |
| 4794 | return false; |
| 4795 | } |
| 4796 | |
| 4797 | return writer->WriteBytesToUInt64(offset_length, offset); |
| 4798 | } |
| 4799 | |
| 4800 | // static |
| 4801 | bool QuicFramer::AppendAckBlock(uint8_t gap, |
| 4802 | QuicPacketNumberLength length_length, |
| 4803 | uint64_t length, |
| 4804 | QuicDataWriter* writer) { |
| 4805 | if (length == 0) { |
| 4806 | if (!IsValidPacketNumberLength(length_length)) { |
| 4807 | QUIC_BUG << "Invalid packet_number_length: " << length_length; |
| 4808 | return false; |
| 4809 | } |
| 4810 | return writer->WriteUInt8(gap) && |
| 4811 | writer->WriteBytesToUInt64(length_length, length); |
| 4812 | } |
| 4813 | return writer->WriteUInt8(gap) && |
| 4814 | AppendPacketNumber(length_length, QuicPacketNumber(length), writer); |
| 4815 | } |
| 4816 | |
| 4817 | bool QuicFramer::AppendStreamFrame(const QuicStreamFrame& frame, |
| 4818 | bool no_stream_frame_length, |
| 4819 | QuicDataWriter* writer) { |
| 4820 | if (version_.transport_version == QUIC_VERSION_99) { |
| 4821 | return AppendIetfStreamFrame(frame, no_stream_frame_length, writer); |
| 4822 | } |
| 4823 | if (!AppendStreamId(GetStreamIdSize(frame.stream_id), frame.stream_id, |
| 4824 | writer)) { |
| 4825 | QUIC_BUG << "Writing stream id size failed."; |
| 4826 | return false; |
| 4827 | } |
| 4828 | if (!AppendStreamOffset( |
| 4829 | GetStreamOffsetSize(version_.transport_version, frame.offset), |
| 4830 | frame.offset, writer)) { |
| 4831 | QUIC_BUG << "Writing offset size failed."; |
| 4832 | return false; |
| 4833 | } |
| 4834 | if (!no_stream_frame_length) { |
| 4835 | if ((frame.data_length > std::numeric_limits<uint16_t>::max()) || |
| 4836 | !writer->WriteUInt16(static_cast<uint16_t>(frame.data_length))) { |
| 4837 | QUIC_BUG << "Writing stream frame length failed"; |
| 4838 | return false; |
| 4839 | } |
| 4840 | } |
| 4841 | |
| 4842 | if (data_producer_ != nullptr) { |
| 4843 | DCHECK_EQ(nullptr, frame.data_buffer); |
| 4844 | if (frame.data_length == 0) { |
| 4845 | return true; |
| 4846 | } |
| 4847 | if (data_producer_->WriteStreamData(frame.stream_id, frame.offset, |
| 4848 | frame.data_length, |
| 4849 | writer) != WRITE_SUCCESS) { |
| 4850 | QUIC_BUG << "Writing frame data failed."; |
| 4851 | return false; |
| 4852 | } |
| 4853 | return true; |
| 4854 | } |
| 4855 | |
| 4856 | if (!writer->WriteBytes(frame.data_buffer, frame.data_length)) { |
| 4857 | QUIC_BUG << "Writing frame data failed."; |
| 4858 | return false; |
| 4859 | } |
| 4860 | return true; |
| 4861 | } |
| 4862 | |
| 4863 | // static |
| 4864 | bool QuicFramer::AppendIetfConnectionId( |
| 4865 | bool version_flag, |
| 4866 | QuicConnectionId destination_connection_id, |
| 4867 | QuicConnectionIdLength destination_connection_id_length, |
| 4868 | QuicConnectionId source_connection_id, |
| 4869 | QuicConnectionIdLength source_connection_id_length, |
| 4870 | QuicDataWriter* writer) { |
| 4871 | if (version_flag) { |
| 4872 | // Append connection ID length byte. |
| 4873 | uint8_t dcil = GetConnectionIdLengthValue(destination_connection_id_length); |
| 4874 | uint8_t scil = GetConnectionIdLengthValue(source_connection_id_length); |
| 4875 | uint8_t connection_id_length = dcil << 4 | scil; |
| 4876 | if (!writer->WriteBytes(&connection_id_length, 1)) { |
| 4877 | return false; |
| 4878 | } |
| 4879 | } |
| 4880 | if (destination_connection_id_length == PACKET_8BYTE_CONNECTION_ID && |
| 4881 | !writer->WriteConnectionId(destination_connection_id)) { |
| 4882 | return false; |
| 4883 | } |
| 4884 | if (source_connection_id_length == PACKET_8BYTE_CONNECTION_ID && |
| 4885 | !writer->WriteConnectionId(source_connection_id)) { |
| 4886 | return false; |
| 4887 | } |
| 4888 | return true; |
| 4889 | } |
| 4890 | |
| 4891 | bool QuicFramer::AppendNewTokenFrame(const QuicNewTokenFrame& frame, |
| 4892 | QuicDataWriter* writer) { |
| 4893 | if (!writer->WriteVarInt62(static_cast<uint64_t>(frame.token.length()))) { |
| 4894 | set_detailed_error("Writing token length failed."); |
| 4895 | return false; |
| 4896 | } |
| 4897 | if (!writer->WriteBytes(frame.token.data(), frame.token.length())) { |
| 4898 | set_detailed_error("Writing token buffer failed."); |
| 4899 | return false; |
| 4900 | } |
| 4901 | return true; |
| 4902 | } |
| 4903 | |
| 4904 | bool QuicFramer::ProcessNewTokenFrame(QuicDataReader* reader, |
| 4905 | QuicNewTokenFrame* frame) { |
| 4906 | uint64_t length; |
| 4907 | if (!reader->ReadVarInt62(&length)) { |
| 4908 | set_detailed_error("Unable to read new token length."); |
| 4909 | return false; |
| 4910 | } |
| 4911 | if (length > kMaxNewTokenTokenLength) { |
| 4912 | set_detailed_error("Token length larger than maximum."); |
| 4913 | return false; |
| 4914 | } |
| 4915 | |
| 4916 | // TODO(ianswett): Don't use QuicStringPiece as an intermediary. |
| 4917 | QuicStringPiece data; |
| 4918 | if (!reader->ReadStringPiece(&data, length)) { |
| 4919 | set_detailed_error("Unable to read new token data."); |
| 4920 | return false; |
| 4921 | } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 4922 | frame->token = std::string(data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 4923 | return true; |
| 4924 | } |
| 4925 | |
| 4926 | // Add a new ietf-format stream frame. |
| 4927 | // Bits controlling whether there is a frame-length and frame-offset |
| 4928 | // are in the QuicStreamFrame. |
| 4929 | bool QuicFramer::AppendIetfStreamFrame(const QuicStreamFrame& frame, |
| 4930 | bool last_frame_in_packet, |
| 4931 | QuicDataWriter* writer) { |
| 4932 | if (!writer->WriteVarInt62(static_cast<uint64_t>(frame.stream_id))) { |
| 4933 | set_detailed_error("Writing stream id failed."); |
| 4934 | return false; |
| 4935 | } |
| 4936 | |
| 4937 | if (frame.offset != 0) { |
| 4938 | if (!writer->WriteVarInt62(static_cast<uint64_t>(frame.offset))) { |
| 4939 | set_detailed_error("Writing data offset failed."); |
| 4940 | return false; |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | if (!last_frame_in_packet) { |
| 4945 | if (!writer->WriteVarInt62(frame.data_length)) { |
| 4946 | set_detailed_error("Writing data length failed."); |
| 4947 | return false; |
| 4948 | } |
| 4949 | } |
| 4950 | |
| 4951 | if (frame.data_length == 0) { |
| 4952 | return true; |
| 4953 | } |
| 4954 | if (data_producer_ == nullptr) { |
| 4955 | if (!writer->WriteBytes(frame.data_buffer, frame.data_length)) { |
| 4956 | set_detailed_error("Writing frame data failed."); |
| 4957 | return false; |
| 4958 | } |
| 4959 | } else { |
| 4960 | DCHECK_EQ(nullptr, frame.data_buffer); |
| 4961 | |
| 4962 | if (data_producer_->WriteStreamData(frame.stream_id, frame.offset, |
| 4963 | frame.data_length, |
| 4964 | writer) != WRITE_SUCCESS) { |
| 4965 | set_detailed_error("Writing frame data failed."); |
| 4966 | return false; |
| 4967 | } |
| 4968 | } |
| 4969 | return true; |
| 4970 | } |
| 4971 | |
| 4972 | bool QuicFramer::AppendCryptoFrame(const QuicCryptoFrame& frame, |
| 4973 | QuicDataWriter* writer) { |
| 4974 | if (!writer->WriteVarInt62(static_cast<uint64_t>(frame.offset))) { |
| 4975 | set_detailed_error("Writing data offset failed."); |
| 4976 | return false; |
| 4977 | } |
| 4978 | if (!writer->WriteVarInt62(static_cast<uint64_t>(frame.data_length))) { |
| 4979 | set_detailed_error("Writing data length failed."); |
| 4980 | return false; |
| 4981 | } |
| 4982 | if (data_producer_ == nullptr) { |
| 4983 | if (frame.data_buffer == nullptr || |
| 4984 | !writer->WriteBytes(frame.data_buffer, frame.data_length)) { |
| 4985 | set_detailed_error("Writing frame data failed."); |
| 4986 | return false; |
| 4987 | } |
| 4988 | } else { |
| 4989 | DCHECK_EQ(nullptr, frame.data_buffer); |
| 4990 | if (!data_producer_->WriteCryptoData(frame.level, frame.offset, |
| 4991 | frame.data_length, writer)) { |
| 4992 | return false; |
| 4993 | } |
| 4994 | } |
| 4995 | return true; |
| 4996 | } |
| 4997 | |
| 4998 | void QuicFramer::set_version(const ParsedQuicVersion version) { |
| 4999 | DCHECK(IsSupportedVersion(version)) << ParsedQuicVersionToString(version); |
| 5000 | version_ = version; |
| 5001 | } |
| 5002 | |
| 5003 | bool QuicFramer::AppendAckFrameAndTypeByte(const QuicAckFrame& frame, |
| 5004 | QuicDataWriter* writer) { |
| 5005 | if (transport_version() == QUIC_VERSION_99) { |
| 5006 | return AppendIetfAckFrameAndTypeByte(frame, writer); |
| 5007 | } |
| 5008 | |
| 5009 | const AckFrameInfo new_ack_info = GetAckFrameInfo(frame); |
| 5010 | QuicPacketNumber largest_acked = LargestAcked(frame); |
| 5011 | QuicPacketNumberLength largest_acked_length = |
| 5012 | GetMinPacketNumberLength(version_.transport_version, largest_acked); |
| 5013 | QuicPacketNumberLength ack_block_length = |
| 5014 | GetMinPacketNumberLength(version_.transport_version, |
| 5015 | QuicPacketNumber(new_ack_info.max_block_length)); |
| 5016 | // Calculate available bytes for timestamps and ack blocks. |
| 5017 | int32_t available_timestamp_and_ack_block_bytes = |
| 5018 | writer->capacity() - writer->length() - ack_block_length - |
| 5019 | GetMinAckFrameSize(version_.transport_version, largest_acked_length) - |
| 5020 | (new_ack_info.num_ack_blocks != 0 ? kNumberOfAckBlocksSize : 0); |
| 5021 | DCHECK_LE(0, available_timestamp_and_ack_block_bytes); |
| 5022 | |
| 5023 | // Write out the type byte by setting the low order bits and doing shifts |
| 5024 | // to make room for the next bit flags to be set. |
| 5025 | // Whether there are multiple ack blocks. |
| 5026 | uint8_t type_byte = 0; |
| 5027 | SetBit(&type_byte, new_ack_info.num_ack_blocks != 0, |
| 5028 | kQuicHasMultipleAckBlocksOffset); |
| 5029 | |
| 5030 | SetBits(&type_byte, GetPacketNumberFlags(largest_acked_length), |
| 5031 | kQuicSequenceNumberLengthNumBits, kLargestAckedOffset); |
| 5032 | |
| 5033 | SetBits(&type_byte, GetPacketNumberFlags(ack_block_length), |
| 5034 | kQuicSequenceNumberLengthNumBits, kActBlockLengthOffset); |
| 5035 | |
| 5036 | type_byte |= kQuicFrameTypeAckMask; |
| 5037 | |
| 5038 | if (!writer->WriteUInt8(type_byte)) { |
| 5039 | return false; |
| 5040 | } |
| 5041 | |
| 5042 | size_t max_num_ack_blocks = available_timestamp_and_ack_block_bytes / |
| 5043 | (ack_block_length + PACKET_1BYTE_PACKET_NUMBER); |
| 5044 | |
| 5045 | // Number of ack blocks. |
| 5046 | size_t num_ack_blocks = |
| 5047 | std::min(new_ack_info.num_ack_blocks, max_num_ack_blocks); |
| 5048 | if (num_ack_blocks > std::numeric_limits<uint8_t>::max()) { |
| 5049 | num_ack_blocks = std::numeric_limits<uint8_t>::max(); |
| 5050 | } |
| 5051 | |
| 5052 | // Largest acked. |
| 5053 | if (!AppendPacketNumber(largest_acked_length, largest_acked, writer)) { |
| 5054 | return false; |
| 5055 | } |
| 5056 | |
| 5057 | // Largest acked delta time. |
| 5058 | uint64_t ack_delay_time_us = kUFloat16MaxValue; |
| 5059 | if (!frame.ack_delay_time.IsInfinite()) { |
| 5060 | DCHECK_LE(0u, frame.ack_delay_time.ToMicroseconds()); |
| 5061 | ack_delay_time_us = frame.ack_delay_time.ToMicroseconds(); |
| 5062 | } |
| 5063 | if (!writer->WriteUFloat16(ack_delay_time_us)) { |
| 5064 | return false; |
| 5065 | } |
| 5066 | |
| 5067 | if (num_ack_blocks > 0) { |
| 5068 | if (!writer->WriteBytes(&num_ack_blocks, 1)) { |
| 5069 | return false; |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | // First ack block length. |
| 5074 | if (!AppendPacketNumber(ack_block_length, |
| 5075 | QuicPacketNumber(new_ack_info.first_block_length), |
| 5076 | writer)) { |
| 5077 | return false; |
| 5078 | } |
| 5079 | |
| 5080 | // Ack blocks. |
| 5081 | if (num_ack_blocks > 0) { |
| 5082 | size_t num_ack_blocks_written = 0; |
| 5083 | // Append, in descending order from the largest ACKed packet, a series of |
| 5084 | // ACK blocks that represents the successfully acknoweldged packets. Each |
| 5085 | // appended gap/block length represents a descending delta from the previous |
| 5086 | // block. i.e.: |
| 5087 | // |--- length ---|--- gap ---|--- length ---|--- gap ---|--- largest ---| |
| 5088 | // For gaps larger than can be represented by a single encoded gap, a 0 |
| 5089 | // length gap of the maximum is used, i.e.: |
| 5090 | // |--- length ---|--- gap ---|- 0 -|--- gap ---|--- largest ---| |
| 5091 | auto itr = frame.packets.rbegin(); |
| 5092 | QuicPacketNumber previous_start = itr->min(); |
| 5093 | ++itr; |
| 5094 | |
| 5095 | for (; |
| 5096 | itr != frame.packets.rend() && num_ack_blocks_written < num_ack_blocks; |
| 5097 | previous_start = itr->min(), ++itr) { |
| 5098 | const auto& interval = *itr; |
| 5099 | const uint64_t total_gap = previous_start - interval.max(); |
| 5100 | const size_t num_encoded_gaps = |
| 5101 | (total_gap + std::numeric_limits<uint8_t>::max() - 1) / |
| 5102 | std::numeric_limits<uint8_t>::max(); |
| 5103 | DCHECK_LE(0u, num_encoded_gaps); |
| 5104 | |
| 5105 | // Append empty ACK blocks because the gap is longer than a single gap. |
| 5106 | for (size_t i = 1; |
| 5107 | i < num_encoded_gaps && num_ack_blocks_written < num_ack_blocks; |
| 5108 | ++i) { |
| 5109 | if (!AppendAckBlock(std::numeric_limits<uint8_t>::max(), |
| 5110 | ack_block_length, 0, writer)) { |
| 5111 | return false; |
| 5112 | } |
| 5113 | ++num_ack_blocks_written; |
| 5114 | } |
| 5115 | if (num_ack_blocks_written >= num_ack_blocks) { |
| 5116 | if (QUIC_PREDICT_FALSE(num_ack_blocks_written != num_ack_blocks)) { |
| 5117 | QUIC_BUG << "Wrote " << num_ack_blocks_written |
| 5118 | << ", expected to write " << num_ack_blocks; |
| 5119 | } |
| 5120 | break; |
| 5121 | } |
| 5122 | |
| 5123 | const uint8_t last_gap = |
| 5124 | total_gap - |
| 5125 | (num_encoded_gaps - 1) * std::numeric_limits<uint8_t>::max(); |
| 5126 | // Append the final ACK block with a non-empty size. |
| 5127 | if (!AppendAckBlock(last_gap, ack_block_length, |
| 5128 | PacketNumberIntervalLength(interval), writer)) { |
| 5129 | return false; |
| 5130 | } |
| 5131 | ++num_ack_blocks_written; |
| 5132 | } |
| 5133 | DCHECK_EQ(num_ack_blocks, num_ack_blocks_written); |
| 5134 | } |
| 5135 | // Timestamps. |
| 5136 | // If we don't process timestamps or if we don't have enough available space |
| 5137 | // to append all the timestamps, don't append any of them. |
| 5138 | if (process_timestamps_ && writer->capacity() - writer->length() >= |
| 5139 | GetAckFrameTimeStampSize(frame)) { |
| 5140 | if (!AppendTimestampsToAckFrame(frame, writer)) { |
| 5141 | return false; |
| 5142 | } |
| 5143 | } else { |
| 5144 | uint8_t num_received_packets = 0; |
| 5145 | if (!writer->WriteBytes(&num_received_packets, 1)) { |
| 5146 | return false; |
| 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | return true; |
| 5151 | } |
| 5152 | |
| 5153 | bool QuicFramer::AppendTimestampsToAckFrame(const QuicAckFrame& frame, |
| 5154 | QuicDataWriter* writer) { |
| 5155 | DCHECK_GE(std::numeric_limits<uint8_t>::max(), |
| 5156 | frame.received_packet_times.size()); |
| 5157 | // num_received_packets is only 1 byte. |
| 5158 | if (frame.received_packet_times.size() > |
| 5159 | std::numeric_limits<uint8_t>::max()) { |
| 5160 | return false; |
| 5161 | } |
| 5162 | |
| 5163 | uint8_t num_received_packets = frame.received_packet_times.size(); |
| 5164 | if (!writer->WriteBytes(&num_received_packets, 1)) { |
| 5165 | return false; |
| 5166 | } |
| 5167 | if (num_received_packets == 0) { |
| 5168 | return true; |
| 5169 | } |
| 5170 | |
| 5171 | auto it = frame.received_packet_times.begin(); |
| 5172 | QuicPacketNumber packet_number = it->first; |
| 5173 | uint64_t delta_from_largest_observed = LargestAcked(frame) - packet_number; |
| 5174 | |
| 5175 | DCHECK_GE(std::numeric_limits<uint8_t>::max(), delta_from_largest_observed); |
| 5176 | if (delta_from_largest_observed > std::numeric_limits<uint8_t>::max()) { |
| 5177 | return false; |
| 5178 | } |
| 5179 | |
| 5180 | if (!writer->WriteUInt8(delta_from_largest_observed)) { |
| 5181 | return false; |
| 5182 | } |
| 5183 | |
| 5184 | // Use the lowest 4 bytes of the time delta from the creation_time_. |
| 5185 | const uint64_t time_epoch_delta_us = UINT64_C(1) << 32; |
| 5186 | uint32_t time_delta_us = |
| 5187 | static_cast<uint32_t>((it->second - creation_time_).ToMicroseconds() & |
| 5188 | (time_epoch_delta_us - 1)); |
| 5189 | if (!writer->WriteUInt32(time_delta_us)) { |
| 5190 | return false; |
| 5191 | } |
| 5192 | |
| 5193 | QuicTime prev_time = it->second; |
| 5194 | |
| 5195 | for (++it; it != frame.received_packet_times.end(); ++it) { |
| 5196 | packet_number = it->first; |
| 5197 | delta_from_largest_observed = LargestAcked(frame) - packet_number; |
| 5198 | |
| 5199 | if (delta_from_largest_observed > std::numeric_limits<uint8_t>::max()) { |
| 5200 | return false; |
| 5201 | } |
| 5202 | |
| 5203 | if (!writer->WriteUInt8(delta_from_largest_observed)) { |
| 5204 | return false; |
| 5205 | } |
| 5206 | |
| 5207 | uint64_t frame_time_delta_us = (it->second - prev_time).ToMicroseconds(); |
| 5208 | prev_time = it->second; |
| 5209 | if (!writer->WriteUFloat16(frame_time_delta_us)) { |
| 5210 | return false; |
| 5211 | } |
| 5212 | } |
| 5213 | return true; |
| 5214 | } |
| 5215 | |
| 5216 | bool QuicFramer::AppendStopWaitingFrame(const QuicPacketHeader& header, |
| 5217 | const QuicStopWaitingFrame& frame, |
| 5218 | QuicDataWriter* writer) { |
| 5219 | DCHECK_GE(QUIC_VERSION_43, version_.transport_version); |
| 5220 | DCHECK(frame.least_unacked.IsInitialized() && |
| 5221 | header.packet_number >= frame.least_unacked); |
| 5222 | const uint64_t least_unacked_delta = |
| 5223 | header.packet_number - frame.least_unacked; |
| 5224 | const uint64_t length_shift = header.packet_number_length * 8; |
| 5225 | |
| 5226 | if (least_unacked_delta >> length_shift > 0) { |
| 5227 | QUIC_BUG << "packet_number_length " << header.packet_number_length |
| 5228 | << " is too small for least_unacked_delta: " << least_unacked_delta |
| 5229 | << " packet_number:" << header.packet_number |
| 5230 | << " least_unacked:" << frame.least_unacked |
| 5231 | << " version:" << version_.transport_version; |
| 5232 | return false; |
| 5233 | } |
| 5234 | if (least_unacked_delta == 0) { |
| 5235 | return writer->WriteBytesToUInt64(header.packet_number_length, |
| 5236 | least_unacked_delta); |
| 5237 | } |
| 5238 | if (!AppendPacketNumber(header.packet_number_length, |
| 5239 | QuicPacketNumber(least_unacked_delta), writer)) { |
| 5240 | QUIC_BUG << " seq failed: " << header.packet_number_length; |
| 5241 | return false; |
| 5242 | } |
| 5243 | |
| 5244 | return true; |
| 5245 | } |
| 5246 | |
| 5247 | int QuicFramer::CalculateIetfAckBlockCount(const QuicAckFrame& frame, |
| 5248 | QuicDataWriter* writer, |
| 5249 | size_t available_space) { |
| 5250 | // Number of blocks requested in the frame |
| 5251 | uint64_t ack_block_count = frame.packets.NumIntervals(); |
| 5252 | |
| 5253 | auto itr = frame.packets.rbegin(); |
| 5254 | |
| 5255 | int actual_block_count = 1; |
| 5256 | uint64_t block_length = itr->max() - itr->min(); |
| 5257 | size_t encoded_size = QuicDataWriter::GetVarInt62Len(block_length); |
| 5258 | if (encoded_size > available_space) { |
| 5259 | return 0; |
| 5260 | } |
| 5261 | available_space -= encoded_size; |
| 5262 | QuicPacketNumber previous_ack_end = itr->min(); |
| 5263 | ack_block_count--; |
| 5264 | |
| 5265 | while (ack_block_count) { |
| 5266 | // Each block is a gap followed by another ACK. Calculate each value, |
| 5267 | // determine the encoded lengths, and check against the available space. |
| 5268 | itr++; |
| 5269 | size_t gap = previous_ack_end - itr->max() - 1; |
| 5270 | encoded_size = QuicDataWriter::GetVarInt62Len(gap); |
| 5271 | |
| 5272 | // Add the ACK block. |
| 5273 | block_length = itr->max() - itr->min(); |
| 5274 | encoded_size += QuicDataWriter::GetVarInt62Len(block_length); |
| 5275 | |
| 5276 | if (encoded_size > available_space) { |
| 5277 | // No room for this block, so what we've |
| 5278 | // done up to now is all that can be done. |
| 5279 | return actual_block_count; |
| 5280 | } |
| 5281 | available_space -= encoded_size; |
| 5282 | actual_block_count++; |
| 5283 | previous_ack_end = itr->min(); |
| 5284 | ack_block_count--; |
| 5285 | } |
| 5286 | // Ran through the whole thing! We can do all blocks. |
| 5287 | return actual_block_count; |
| 5288 | } |
| 5289 | |
| 5290 | bool QuicFramer::AppendIetfAckFrameAndTypeByte(const QuicAckFrame& frame, |
| 5291 | QuicDataWriter* writer) { |
| 5292 | // Assume frame is an IETF_ACK frame. If |ecn_counters_populated| is true and |
| 5293 | // any of the ECN counters is non-0 then turn it into an IETF_ACK+ECN frame. |
| 5294 | uint8_t type = IETF_ACK; |
| 5295 | if (frame.ecn_counters_populated && |
| 5296 | (frame.ect_0_count || frame.ect_1_count || frame.ecn_ce_count)) { |
| 5297 | type = IETF_ACK_ECN; |
| 5298 | } |
| 5299 | |
| 5300 | if (!writer->WriteUInt8(type)) { |
| 5301 | set_detailed_error("No room for frame-type"); |
| 5302 | return false; |
| 5303 | } |
| 5304 | |
| 5305 | QuicPacketNumber largest_acked = LargestAcked(frame); |
| 5306 | if (!writer->WriteVarInt62(largest_acked.ToUint64())) { |
| 5307 | set_detailed_error("No room for largest-acked in ack frame"); |
| 5308 | return false; |
| 5309 | } |
| 5310 | |
| 5311 | uint64_t ack_delay_time_us = kVarInt62MaxValue; |
| 5312 | if (!frame.ack_delay_time.IsInfinite()) { |
| 5313 | DCHECK_LE(0u, frame.ack_delay_time.ToMicroseconds()); |
| 5314 | ack_delay_time_us = frame.ack_delay_time.ToMicroseconds(); |
| 5315 | // TODO(fkastenholz): Use the shift from TLS transport parameters. |
| 5316 | ack_delay_time_us = ack_delay_time_us >> kIetfAckTimestampShift; |
| 5317 | } |
| 5318 | |
| 5319 | if (!writer->WriteVarInt62(ack_delay_time_us)) { |
| 5320 | set_detailed_error("No room for ack-delay in ack frame"); |
| 5321 | return false; |
| 5322 | } |
| 5323 | if (type == IETF_ACK_ECN) { |
| 5324 | // Encode the ACK ECN fields |
| 5325 | if (!writer->WriteVarInt62(frame.ect_0_count)) { |
| 5326 | set_detailed_error("No room for ect_0_count in ack frame"); |
| 5327 | return false; |
| 5328 | } |
| 5329 | if (!writer->WriteVarInt62(frame.ect_1_count)) { |
| 5330 | set_detailed_error("No room for ect_1_count in ack frame"); |
| 5331 | return false; |
| 5332 | } |
| 5333 | if (!writer->WriteVarInt62(frame.ecn_ce_count)) { |
| 5334 | set_detailed_error("No room for ecn_ce_count in ack frame"); |
| 5335 | return false; |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | uint64_t ack_block_count = frame.packets.NumIntervals(); |
| 5340 | if (ack_block_count == 0) { |
| 5341 | // If the QuicAckFrame has no Intervals, then it is interpreted |
| 5342 | // as an ack of a single packet at QuicAckFrame.largest_acked. |
| 5343 | // The resulting ack will consist of only the frame's |
| 5344 | // largest_ack & first_ack_block fields. The first ack block will be 0 |
| 5345 | // (indicating a single packet) and the ack block_count will be 0. |
| 5346 | if (!writer->WriteVarInt62(0)) { |
| 5347 | set_detailed_error("No room for ack block count in ack frame"); |
| 5348 | return false; |
| 5349 | } |
| 5350 | // size of the first block is 1 packet |
| 5351 | if (!writer->WriteVarInt62(0)) { |
| 5352 | set_detailed_error("No room for first ack block in ack frame"); |
| 5353 | return false; |
| 5354 | } |
| 5355 | return true; |
| 5356 | } |
| 5357 | // Case 2 or 3 |
| 5358 | auto itr = frame.packets.rbegin(); |
| 5359 | |
| 5360 | QuicPacketNumber ack_block_largest(largest_acked); |
| 5361 | QuicPacketNumber ack_block_smallest; |
| 5362 | if ((itr->max() - 1) == QuicPacketNumber(largest_acked)) { |
| 5363 | // If largest_acked + 1 is equal to the Max() of the first Interval |
| 5364 | // in the QuicAckFrame then the first Interval is the first ack block of the |
| 5365 | // frame; remaining Intervals are additional ack blocks. The QuicAckFrame's |
| 5366 | // first Interval is encoded in the frame's largest_acked/first_ack_block, |
| 5367 | // the remaining Intervals are encoded in additional ack blocks in the |
| 5368 | // frame, and the packet's ack_block_count is the number of QuicAckFrame |
| 5369 | // Intervals - 1. |
| 5370 | ack_block_smallest = itr->min(); |
| 5371 | itr++; |
| 5372 | ack_block_count--; |
| 5373 | } else { |
| 5374 | // If QuicAckFrame.largest_acked is NOT equal to the Max() of |
| 5375 | // the first Interval then it is interpreted as acking a single |
| 5376 | // packet at QuicAckFrame.largest_acked, with additional |
| 5377 | // Intervals indicating additional ack blocks. The encoding is |
| 5378 | // a) The packet's largest_acked is the QuicAckFrame's largest |
| 5379 | // acked, |
| 5380 | // b) the first ack block size is 0, |
| 5381 | // c) The packet's ack_block_count is the number of QuicAckFrame |
| 5382 | // Intervals, and |
| 5383 | // d) The QuicAckFrame Intervals are encoded in additional ack |
| 5384 | // blocks in the packet. |
| 5385 | ack_block_smallest = largest_acked; |
| 5386 | } |
| 5387 | |
| 5388 | if (!writer->WriteVarInt62(ack_block_count)) { |
| 5389 | set_detailed_error("No room for ack block count in ack frame"); |
| 5390 | return false; |
| 5391 | } |
| 5392 | |
| 5393 | uint64_t first_ack_block = ack_block_largest - ack_block_smallest; |
| 5394 | if (!writer->WriteVarInt62(first_ack_block)) { |
| 5395 | set_detailed_error("No room for first ack block in ack frame"); |
| 5396 | return false; |
| 5397 | } |
| 5398 | |
| 5399 | // For the remaining QuicAckFrame Intervals, if any |
| 5400 | while (ack_block_count != 0) { |
| 5401 | uint64_t gap_size = ack_block_smallest - itr->max(); |
| 5402 | if (!writer->WriteVarInt62(gap_size - 1)) { |
| 5403 | set_detailed_error("No room for gap block in ack frame"); |
| 5404 | return false; |
| 5405 | } |
| 5406 | |
| 5407 | uint64_t block_size = itr->max() - itr->min(); |
| 5408 | if (!writer->WriteVarInt62(block_size - 1)) { |
| 5409 | set_detailed_error("No room for nth ack block in ack frame"); |
| 5410 | return false; |
| 5411 | } |
| 5412 | |
| 5413 | ack_block_smallest = itr->min(); |
| 5414 | itr++; |
| 5415 | ack_block_count--; |
| 5416 | } |
| 5417 | return true; |
| 5418 | } |
| 5419 | |
| 5420 | bool QuicFramer::AppendRstStreamFrame(const QuicRstStreamFrame& frame, |
| 5421 | QuicDataWriter* writer) { |
| 5422 | if (version_.transport_version == QUIC_VERSION_99) { |
| 5423 | return AppendIetfResetStreamFrame(frame, writer); |
| 5424 | } |
| 5425 | if (!writer->WriteUInt32(frame.stream_id)) { |
| 5426 | return false; |
| 5427 | } |
| 5428 | |
| 5429 | if (!writer->WriteUInt64(frame.byte_offset)) { |
| 5430 | return false; |
| 5431 | } |
| 5432 | |
| 5433 | uint32_t error_code = static_cast<uint32_t>(frame.error_code); |
| 5434 | if (!writer->WriteUInt32(error_code)) { |
| 5435 | return false; |
| 5436 | } |
| 5437 | |
| 5438 | return true; |
| 5439 | } |
| 5440 | |
| 5441 | bool QuicFramer::AppendConnectionCloseFrame( |
| 5442 | const QuicConnectionCloseFrame& frame, |
| 5443 | QuicDataWriter* writer) { |
| 5444 | if (version_.transport_version == QUIC_VERSION_99) { |
| 5445 | return AppendIetfConnectionCloseFrame(frame, writer); |
| 5446 | } |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 5447 | uint32_t error_code = static_cast<uint32_t>(frame.quic_error_code); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5448 | if (!writer->WriteUInt32(error_code)) { |
| 5449 | return false; |
| 5450 | } |
| 5451 | if (!writer->WriteStringPiece16(TruncateErrorString(frame.error_details))) { |
| 5452 | return false; |
| 5453 | } |
| 5454 | return true; |
| 5455 | } |
| 5456 | |
| 5457 | bool QuicFramer::AppendGoAwayFrame(const QuicGoAwayFrame& frame, |
| 5458 | QuicDataWriter* writer) { |
| 5459 | uint32_t error_code = static_cast<uint32_t>(frame.error_code); |
| 5460 | if (!writer->WriteUInt32(error_code)) { |
| 5461 | return false; |
| 5462 | } |
| 5463 | uint32_t stream_id = static_cast<uint32_t>(frame.last_good_stream_id); |
| 5464 | if (!writer->WriteUInt32(stream_id)) { |
| 5465 | return false; |
| 5466 | } |
| 5467 | if (!writer->WriteStringPiece16(TruncateErrorString(frame.reason_phrase))) { |
| 5468 | return false; |
| 5469 | } |
| 5470 | return true; |
| 5471 | } |
| 5472 | |
| 5473 | bool QuicFramer::AppendWindowUpdateFrame(const QuicWindowUpdateFrame& frame, |
| 5474 | QuicDataWriter* writer) { |
| 5475 | uint32_t stream_id = static_cast<uint32_t>(frame.stream_id); |
| 5476 | if (!writer->WriteUInt32(stream_id)) { |
| 5477 | return false; |
| 5478 | } |
| 5479 | if (!writer->WriteUInt64(frame.byte_offset)) { |
| 5480 | return false; |
| 5481 | } |
| 5482 | return true; |
| 5483 | } |
| 5484 | |
| 5485 | bool QuicFramer::AppendBlockedFrame(const QuicBlockedFrame& frame, |
| 5486 | QuicDataWriter* writer) { |
| 5487 | if (version_.transport_version == QUIC_VERSION_99) { |
| 5488 | if (frame.stream_id == QuicUtils::GetInvalidStreamId(transport_version())) { |
| 5489 | return AppendIetfBlockedFrame(frame, writer); |
| 5490 | } |
| 5491 | return AppendStreamBlockedFrame(frame, writer); |
| 5492 | } |
| 5493 | uint32_t stream_id = static_cast<uint32_t>(frame.stream_id); |
| 5494 | if (!writer->WriteUInt32(stream_id)) { |
| 5495 | return false; |
| 5496 | } |
| 5497 | return true; |
| 5498 | } |
| 5499 | |
| 5500 | bool QuicFramer::AppendPaddingFrame(const QuicPaddingFrame& frame, |
| 5501 | QuicDataWriter* writer) { |
| 5502 | if (frame.num_padding_bytes == 0) { |
| 5503 | return false; |
| 5504 | } |
| 5505 | if (frame.num_padding_bytes < 0) { |
| 5506 | QUIC_BUG_IF(frame.num_padding_bytes != -1); |
| 5507 | writer->WritePadding(); |
| 5508 | return true; |
| 5509 | } |
| 5510 | // Please note, num_padding_bytes includes type byte which has been written. |
| 5511 | return writer->WritePaddingBytes(frame.num_padding_bytes - 1); |
| 5512 | } |
| 5513 | |
| 5514 | bool QuicFramer::AppendMessageFrameAndTypeByte(const QuicMessageFrame& frame, |
| 5515 | bool last_frame_in_packet, |
| 5516 | QuicDataWriter* writer) { |
| 5517 | uint8_t type_byte = last_frame_in_packet ? IETF_EXTENSION_MESSAGE_NO_LENGTH |
| 5518 | : IETF_EXTENSION_MESSAGE; |
| 5519 | if (!writer->WriteUInt8(type_byte)) { |
| 5520 | return false; |
| 5521 | } |
| 5522 | if (!last_frame_in_packet && !writer->WriteVarInt62(frame.message_length)) { |
| 5523 | return false; |
| 5524 | } |
| 5525 | for (const auto& slice : frame.message_data) { |
| 5526 | if (!writer->WriteBytes(slice.data(), slice.length())) { |
| 5527 | return false; |
| 5528 | } |
| 5529 | } |
| 5530 | return true; |
| 5531 | } |
| 5532 | |
| 5533 | bool QuicFramer::RaiseError(QuicErrorCode error) { |
| 5534 | QUIC_DLOG(INFO) << ENDPOINT << "Error: " << QuicErrorCodeToString(error) |
| 5535 | << " detail: " << detailed_error_; |
| 5536 | set_error(error); |
nharper | 55fa613 | 2019-05-07 19:37:21 -0700 | [diff] [blame] | 5537 | if (visitor_) { |
| 5538 | visitor_->OnError(this); |
| 5539 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5540 | return false; |
| 5541 | } |
| 5542 | |
| 5543 | bool QuicFramer::IsVersionNegotiation( |
| 5544 | const QuicPacketHeader& header, |
| 5545 | bool packet_has_ietf_packet_header) const { |
| 5546 | if (perspective_ == Perspective::IS_SERVER) { |
dschinazi | 072da7c | 2019-05-07 17:57:42 -0700 | [diff] [blame] | 5547 | if (!GetQuicRestartFlag(quic_server_drop_version_negotiation)) { |
| 5548 | return false; |
| 5549 | } |
| 5550 | QUIC_RESTART_FLAG_COUNT_N(quic_server_drop_version_negotiation, 2, 2); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5551 | } |
dschinazi | 072da7c | 2019-05-07 17:57:42 -0700 | [diff] [blame] | 5552 | if (!packet_has_ietf_packet_header && |
| 5553 | perspective_ == Perspective::IS_CLIENT) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5554 | return header.version_flag; |
| 5555 | } |
| 5556 | if (header.form == IETF_QUIC_SHORT_HEADER_PACKET) { |
| 5557 | return false; |
| 5558 | } |
| 5559 | return header.long_packet_type == VERSION_NEGOTIATION; |
| 5560 | } |
| 5561 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5562 | bool QuicFramer::AppendIetfConnectionCloseFrame( |
| 5563 | const QuicConnectionCloseFrame& frame, |
| 5564 | QuicDataWriter* writer) { |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 5565 | if (frame.close_type != IETF_QUIC_TRANSPORT_CONNECTION_CLOSE && |
| 5566 | frame.close_type != IETF_QUIC_APPLICATION_CONNECTION_CLOSE) { |
| 5567 | QUIC_BUG << "Invalid close_type for writing IETF CONNECTION CLOSE."; |
| 5568 | set_detailed_error("Invalid close_type for writing IETF CONNECTION CLOSE."); |
| 5569 | return false; |
| 5570 | } |
| 5571 | |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 5572 | if (!writer->WriteUInt16(frame.application_error_code)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5573 | set_detailed_error("Can not write connection close frame error code"); |
| 5574 | return false; |
| 5575 | } |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 5576 | |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 5577 | if (frame.close_type == IETF_QUIC_TRANSPORT_CONNECTION_CLOSE) { |
| 5578 | // Write the frame-type of the frame causing the error only |
| 5579 | // if it's a CONNECTION_CLOSE/Transport. |
| 5580 | if (!writer->WriteVarInt62(frame.transport_close_frame_type)) { |
| 5581 | set_detailed_error("Writing frame type failed."); |
| 5582 | return false; |
| 5583 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5584 | } |
| 5585 | |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 5586 | // TODO(fkastenholz): For full IETF CONNECTION CLOSE support, |
| 5587 | // if this is a Transport CONNECTION_CLOSE and the extended |
| 5588 | // error is not QUIC_IETF_GQUIC_ERROR_MISSING then append the extended |
| 5589 | // "QuicErrorCode: #" string to the phrase. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5590 | if (!writer->WriteStringPieceVarInt62( |
| 5591 | TruncateErrorString(frame.error_details))) { |
| 5592 | set_detailed_error("Can not write connection close phrase"); |
| 5593 | return false; |
| 5594 | } |
| 5595 | return true; |
| 5596 | } |
| 5597 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5598 | bool QuicFramer::ProcessIetfConnectionCloseFrame( |
| 5599 | QuicDataReader* reader, |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 5600 | QuicConnectionCloseType type, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5601 | QuicConnectionCloseFrame* frame) { |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 5602 | frame->close_type = type; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5603 | uint16_t code; |
| 5604 | if (!reader->ReadUInt16(&code)) { |
| 5605 | set_detailed_error("Unable to read connection close error code."); |
| 5606 | return false; |
| 5607 | } |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 5608 | frame->transport_error_code = static_cast<QuicIetfTransportErrorCodes>(code); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5609 | |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 5610 | if (type == IETF_QUIC_TRANSPORT_CONNECTION_CLOSE) { |
| 5611 | // The frame-type of the frame causing the error is present only |
| 5612 | // if it's a CONNECTION_CLOSE/Transport. |
| 5613 | if (!reader->ReadVarInt62(&frame->transport_close_frame_type)) { |
| 5614 | set_detailed_error("Unable to read connection close frame type."); |
| 5615 | return false; |
| 5616 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5617 | } |
| 5618 | |
| 5619 | uint64_t phrase_length; |
| 5620 | if (!reader->ReadVarInt62(&phrase_length)) { |
| 5621 | set_detailed_error("Unable to read connection close error details."); |
| 5622 | return false; |
| 5623 | } |
| 5624 | QuicStringPiece phrase; |
| 5625 | if (!reader->ReadStringPiece(&phrase, static_cast<size_t>(phrase_length))) { |
| 5626 | set_detailed_error("Unable to read connection close error details."); |
| 5627 | return false; |
| 5628 | } |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 5629 | // TODO(fkastenholz): when full support is done, add code here |
| 5630 | // to extract the extended error code from the reason phrase |
| 5631 | // and set it into frame->extracted_error_code. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 5632 | frame->error_details = std::string(phrase); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5633 | |
| 5634 | return true; |
| 5635 | } |
| 5636 | |
| 5637 | // IETF Quic Path Challenge/Response frames. |
| 5638 | bool QuicFramer::ProcessPathChallengeFrame(QuicDataReader* reader, |
| 5639 | QuicPathChallengeFrame* frame) { |
| 5640 | if (!reader->ReadBytes(frame->data_buffer.data(), |
| 5641 | frame->data_buffer.size())) { |
| 5642 | set_detailed_error("Can not read path challenge data."); |
| 5643 | return false; |
| 5644 | } |
| 5645 | return true; |
| 5646 | } |
| 5647 | |
| 5648 | bool QuicFramer::ProcessPathResponseFrame(QuicDataReader* reader, |
| 5649 | QuicPathResponseFrame* frame) { |
| 5650 | if (!reader->ReadBytes(frame->data_buffer.data(), |
| 5651 | frame->data_buffer.size())) { |
| 5652 | set_detailed_error("Can not read path response data."); |
| 5653 | return false; |
| 5654 | } |
| 5655 | return true; |
| 5656 | } |
| 5657 | |
| 5658 | bool QuicFramer::AppendPathChallengeFrame(const QuicPathChallengeFrame& frame, |
| 5659 | QuicDataWriter* writer) { |
| 5660 | if (!writer->WriteBytes(frame.data_buffer.data(), frame.data_buffer.size())) { |
| 5661 | set_detailed_error("Writing Path Challenge data failed."); |
| 5662 | return false; |
| 5663 | } |
| 5664 | return true; |
| 5665 | } |
| 5666 | |
| 5667 | bool QuicFramer::AppendPathResponseFrame(const QuicPathResponseFrame& frame, |
| 5668 | QuicDataWriter* writer) { |
| 5669 | if (!writer->WriteBytes(frame.data_buffer.data(), frame.data_buffer.size())) { |
| 5670 | set_detailed_error("Writing Path Response data failed."); |
| 5671 | return false; |
| 5672 | } |
| 5673 | return true; |
| 5674 | } |
| 5675 | |
| 5676 | // Add a new ietf-format stream reset frame. |
| 5677 | // General format is |
| 5678 | // stream id |
| 5679 | // application error code |
| 5680 | // final offset |
| 5681 | bool QuicFramer::AppendIetfResetStreamFrame(const QuicRstStreamFrame& frame, |
| 5682 | QuicDataWriter* writer) { |
| 5683 | if (!writer->WriteVarInt62(static_cast<uint64_t>(frame.stream_id))) { |
| 5684 | set_detailed_error("Writing reset-stream stream id failed."); |
| 5685 | return false; |
| 5686 | } |
| 5687 | if (!writer->WriteUInt16(frame.ietf_error_code)) { |
| 5688 | set_detailed_error("Writing reset-stream error code failed."); |
| 5689 | return false; |
| 5690 | } |
| 5691 | if (!writer->WriteVarInt62(static_cast<uint64_t>(frame.byte_offset))) { |
| 5692 | set_detailed_error("Writing reset-stream final-offset failed."); |
| 5693 | return false; |
| 5694 | } |
| 5695 | return true; |
| 5696 | } |
| 5697 | |
| 5698 | bool QuicFramer::ProcessIetfResetStreamFrame(QuicDataReader* reader, |
| 5699 | QuicRstStreamFrame* frame) { |
| 5700 | // Get Stream ID from frame. ReadVarIntStreamID returns false |
| 5701 | // if either A) there is a read error or B) the resulting value of |
| 5702 | // the Stream ID is larger than the maximum allowed value. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5703 | if (!reader->ReadVarIntU32(&frame->stream_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5704 | set_detailed_error("Unable to read rst stream stream id."); |
| 5705 | return false; |
| 5706 | } |
| 5707 | |
| 5708 | if (!reader->ReadUInt16(&frame->ietf_error_code)) { |
| 5709 | set_detailed_error("Unable to read rst stream error code."); |
| 5710 | return false; |
| 5711 | } |
| 5712 | |
| 5713 | if (!reader->ReadVarInt62(&frame->byte_offset)) { |
| 5714 | set_detailed_error("Unable to read rst stream sent byte offset."); |
| 5715 | return false; |
| 5716 | } |
| 5717 | return true; |
| 5718 | } |
| 5719 | |
| 5720 | bool QuicFramer::ProcessStopSendingFrame( |
| 5721 | QuicDataReader* reader, |
| 5722 | QuicStopSendingFrame* stop_sending_frame) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5723 | if (!reader->ReadVarIntU32(&stop_sending_frame->stream_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5724 | set_detailed_error("Unable to read stop sending stream id."); |
| 5725 | return false; |
| 5726 | } |
| 5727 | |
| 5728 | if (!reader->ReadUInt16(&stop_sending_frame->application_error_code)) { |
| 5729 | set_detailed_error("Unable to read stop sending application error code."); |
| 5730 | return false; |
| 5731 | } |
| 5732 | return true; |
| 5733 | } |
| 5734 | |
| 5735 | bool QuicFramer::AppendStopSendingFrame( |
| 5736 | const QuicStopSendingFrame& stop_sending_frame, |
| 5737 | QuicDataWriter* writer) { |
| 5738 | if (!writer->WriteVarInt62(stop_sending_frame.stream_id)) { |
| 5739 | set_detailed_error("Can not write stop sending stream id"); |
| 5740 | return false; |
| 5741 | } |
| 5742 | if (!writer->WriteUInt16(stop_sending_frame.application_error_code)) { |
| 5743 | set_detailed_error("Can not write application error code"); |
| 5744 | return false; |
| 5745 | } |
| 5746 | return true; |
| 5747 | } |
| 5748 | |
| 5749 | // Append/process IETF-Format MAX_DATA Frame |
| 5750 | bool QuicFramer::AppendMaxDataFrame(const QuicWindowUpdateFrame& frame, |
| 5751 | QuicDataWriter* writer) { |
| 5752 | if (!writer->WriteVarInt62(frame.byte_offset)) { |
| 5753 | set_detailed_error("Can not write MAX_DATA byte-offset"); |
| 5754 | return false; |
| 5755 | } |
| 5756 | return true; |
| 5757 | } |
| 5758 | |
| 5759 | bool QuicFramer::ProcessMaxDataFrame(QuicDataReader* reader, |
| 5760 | QuicWindowUpdateFrame* frame) { |
| 5761 | frame->stream_id = QuicUtils::GetInvalidStreamId(transport_version()); |
| 5762 | if (!reader->ReadVarInt62(&frame->byte_offset)) { |
| 5763 | set_detailed_error("Can not read MAX_DATA byte-offset"); |
| 5764 | return false; |
| 5765 | } |
| 5766 | return true; |
| 5767 | } |
| 5768 | |
| 5769 | // Append/process IETF-Format MAX_STREAM_DATA Frame |
| 5770 | bool QuicFramer::AppendMaxStreamDataFrame(const QuicWindowUpdateFrame& frame, |
| 5771 | QuicDataWriter* writer) { |
| 5772 | if (!writer->WriteVarInt62(frame.stream_id)) { |
| 5773 | set_detailed_error("Can not write MAX_STREAM_DATA stream id"); |
| 5774 | return false; |
| 5775 | } |
| 5776 | if (!writer->WriteVarInt62(frame.byte_offset)) { |
| 5777 | set_detailed_error("Can not write MAX_STREAM_DATA byte-offset"); |
| 5778 | return false; |
| 5779 | } |
| 5780 | return true; |
| 5781 | } |
| 5782 | |
| 5783 | bool QuicFramer::ProcessMaxStreamDataFrame(QuicDataReader* reader, |
| 5784 | QuicWindowUpdateFrame* frame) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5785 | if (!reader->ReadVarIntU32(&frame->stream_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5786 | set_detailed_error("Can not read MAX_STREAM_DATA stream id"); |
| 5787 | return false; |
| 5788 | } |
| 5789 | if (!reader->ReadVarInt62(&frame->byte_offset)) { |
| 5790 | set_detailed_error("Can not read MAX_STREAM_DATA byte-count"); |
| 5791 | return false; |
| 5792 | } |
| 5793 | return true; |
| 5794 | } |
| 5795 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5796 | bool QuicFramer::AppendMaxStreamsFrame(const QuicMaxStreamsFrame& frame, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5797 | QuicDataWriter* writer) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5798 | if (!writer->WriteVarInt62(frame.stream_count)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5799 | set_detailed_error("Can not write MAX_STREAMS stream count"); |
| 5800 | return false; |
| 5801 | } |
| 5802 | return true; |
| 5803 | } |
| 5804 | |
| 5805 | bool QuicFramer::ProcessMaxStreamsFrame(QuicDataReader* reader, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5806 | QuicMaxStreamsFrame* frame, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5807 | uint64_t frame_type) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5808 | if (!reader->ReadVarIntU32(&frame->stream_count)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5809 | set_detailed_error("Can not read MAX_STREAMS stream count."); |
| 5810 | return false; |
| 5811 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5812 | frame->unidirectional = (frame_type == IETF_MAX_STREAMS_UNIDIRECTIONAL); |
| 5813 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5814 | } |
| 5815 | |
| 5816 | bool QuicFramer::AppendIetfBlockedFrame(const QuicBlockedFrame& frame, |
| 5817 | QuicDataWriter* writer) { |
| 5818 | if (!writer->WriteVarInt62(frame.offset)) { |
| 5819 | set_detailed_error("Can not write blocked offset."); |
| 5820 | return false; |
| 5821 | } |
| 5822 | return true; |
| 5823 | } |
| 5824 | |
| 5825 | bool QuicFramer::ProcessIetfBlockedFrame(QuicDataReader* reader, |
| 5826 | QuicBlockedFrame* frame) { |
| 5827 | // Indicates that it is a BLOCKED frame (as opposed to STREAM_BLOCKED). |
| 5828 | frame->stream_id = QuicUtils::GetInvalidStreamId(transport_version()); |
| 5829 | if (!reader->ReadVarInt62(&frame->offset)) { |
| 5830 | set_detailed_error("Can not read blocked offset."); |
| 5831 | return false; |
| 5832 | } |
| 5833 | return true; |
| 5834 | } |
| 5835 | |
| 5836 | bool QuicFramer::AppendStreamBlockedFrame(const QuicBlockedFrame& frame, |
| 5837 | QuicDataWriter* writer) { |
| 5838 | if (!writer->WriteVarInt62(frame.stream_id)) { |
| 5839 | set_detailed_error("Can not write stream blocked stream id."); |
| 5840 | return false; |
| 5841 | } |
| 5842 | if (!writer->WriteVarInt62(frame.offset)) { |
| 5843 | set_detailed_error("Can not write stream blocked offset."); |
| 5844 | return false; |
| 5845 | } |
| 5846 | return true; |
| 5847 | } |
| 5848 | |
| 5849 | bool QuicFramer::ProcessStreamBlockedFrame(QuicDataReader* reader, |
| 5850 | QuicBlockedFrame* frame) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5851 | if (!reader->ReadVarIntU32(&frame->stream_id)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5852 | set_detailed_error("Can not read stream blocked stream id."); |
| 5853 | return false; |
| 5854 | } |
| 5855 | if (!reader->ReadVarInt62(&frame->offset)) { |
| 5856 | set_detailed_error("Can not read stream blocked offset."); |
| 5857 | return false; |
| 5858 | } |
| 5859 | return true; |
| 5860 | } |
| 5861 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5862 | bool QuicFramer::AppendStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame, |
| 5863 | QuicDataWriter* writer) { |
| 5864 | if (!writer->WriteVarInt62(frame.stream_count)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5865 | set_detailed_error("Can not write STREAMS_BLOCKED stream count"); |
| 5866 | return false; |
| 5867 | } |
| 5868 | return true; |
| 5869 | } |
| 5870 | |
| 5871 | bool QuicFramer::ProcessStreamsBlockedFrame(QuicDataReader* reader, |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5872 | QuicStreamsBlockedFrame* frame, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5873 | uint64_t frame_type) { |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5874 | if (!reader->ReadVarIntU32(&frame->stream_count)) { |
| 5875 | set_detailed_error("Can not read STREAMS_BLOCKED stream count."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5876 | return false; |
| 5877 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5878 | frame->unidirectional = (frame_type == IETF_STREAMS_BLOCKED_UNIDIRECTIONAL); |
| 5879 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5880 | // TODO(fkastenholz): handle properly when the STREAMS_BLOCKED |
| 5881 | // frame is implemented and passed up to the stream ID manager. |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5882 | if (frame->stream_count > |
| 5883 | QuicUtils::GetMaxStreamCount( |
| 5884 | (frame_type == IETF_STREAMS_BLOCKED_UNIDIRECTIONAL), |
| 5885 | ((perspective_ == Perspective::IS_CLIENT) |
| 5886 | ? Perspective::IS_SERVER |
| 5887 | : Perspective::IS_CLIENT))) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5888 | // If stream count is such that the resulting stream ID would exceed our |
| 5889 | // implementation limit, generate an error. |
| 5890 | set_detailed_error( |
| 5891 | "STREAMS_BLOCKED stream count exceeds implementation limit."); |
| 5892 | return false; |
| 5893 | } |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 5894 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5895 | } |
| 5896 | |
| 5897 | bool QuicFramer::AppendNewConnectionIdFrame( |
| 5898 | const QuicNewConnectionIdFrame& frame, |
| 5899 | QuicDataWriter* writer) { |
| 5900 | if (!writer->WriteVarInt62(frame.sequence_number)) { |
| 5901 | set_detailed_error("Can not write New Connection ID sequence number"); |
| 5902 | return false; |
| 5903 | } |
| 5904 | if (!writer->WriteUInt8(frame.connection_id.length())) { |
| 5905 | set_detailed_error( |
| 5906 | "Can not write New Connection ID frame connection ID Length"); |
| 5907 | return false; |
| 5908 | } |
| 5909 | if (!writer->WriteConnectionId(frame.connection_id)) { |
| 5910 | set_detailed_error("Can not write New Connection ID frame connection ID"); |
| 5911 | return false; |
| 5912 | } |
| 5913 | |
| 5914 | if (!writer->WriteBytes( |
| 5915 | static_cast<const void*>(&frame.stateless_reset_token), |
| 5916 | sizeof(frame.stateless_reset_token))) { |
| 5917 | set_detailed_error("Can not write New Connection ID Reset Token"); |
| 5918 | return false; |
| 5919 | } |
| 5920 | return true; |
| 5921 | } |
| 5922 | |
| 5923 | bool QuicFramer::ProcessNewConnectionIdFrame(QuicDataReader* reader, |
| 5924 | QuicNewConnectionIdFrame* frame) { |
| 5925 | if (!reader->ReadVarInt62(&frame->sequence_number)) { |
| 5926 | set_detailed_error( |
| 5927 | "Unable to read new connection ID frame sequence number."); |
| 5928 | return false; |
| 5929 | } |
| 5930 | |
| 5931 | uint8_t connection_id_length; |
| 5932 | if (!reader->ReadUInt8(&connection_id_length)) { |
| 5933 | set_detailed_error( |
| 5934 | "Unable to read new connection ID frame connection id length."); |
| 5935 | return false; |
| 5936 | } |
| 5937 | |
QUICHE team | 0131a5b | 2019-03-20 15:23:27 -0700 | [diff] [blame] | 5938 | if (connection_id_length > kQuicMaxConnectionIdLength) { |
| 5939 | set_detailed_error("New connection ID length too high."); |
| 5940 | return false; |
| 5941 | } |
| 5942 | |
QUICHE team | 8e2e453 | 2019-03-14 14:37:56 -0700 | [diff] [blame] | 5943 | if (connection_id_length != kQuicDefaultConnectionIdLength && |
| 5944 | !QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
| 5945 | transport_version())) { |
QUICHE team | 0131a5b | 2019-03-20 15:23:27 -0700 | [diff] [blame] | 5946 | set_detailed_error("Invalid new connection ID length for version."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 5947 | return false; |
| 5948 | } |
| 5949 | |
| 5950 | if (!reader->ReadConnectionId(&frame->connection_id, connection_id_length)) { |
| 5951 | set_detailed_error("Unable to read new connection ID frame connection id."); |
| 5952 | return false; |
| 5953 | } |
| 5954 | |
| 5955 | if (!reader->ReadBytes(&frame->stateless_reset_token, |
| 5956 | sizeof(frame->stateless_reset_token))) { |
| 5957 | set_detailed_error("Can not read new connection ID frame reset token."); |
| 5958 | return false; |
| 5959 | } |
| 5960 | return true; |
| 5961 | } |
| 5962 | |
| 5963 | bool QuicFramer::AppendRetireConnectionIdFrame( |
| 5964 | const QuicRetireConnectionIdFrame& frame, |
| 5965 | QuicDataWriter* writer) { |
| 5966 | if (!writer->WriteVarInt62(frame.sequence_number)) { |
| 5967 | set_detailed_error("Can not write Retire Connection ID sequence number"); |
| 5968 | return false; |
| 5969 | } |
| 5970 | return true; |
| 5971 | } |
| 5972 | |
| 5973 | bool QuicFramer::ProcessRetireConnectionIdFrame( |
| 5974 | QuicDataReader* reader, |
| 5975 | QuicRetireConnectionIdFrame* frame) { |
| 5976 | if (!reader->ReadVarInt62(&frame->sequence_number)) { |
| 5977 | set_detailed_error( |
| 5978 | "Unable to read retire connection ID frame sequence number."); |
| 5979 | return false; |
| 5980 | } |
| 5981 | return true; |
| 5982 | } |
| 5983 | |
| 5984 | uint8_t QuicFramer::GetStreamFrameTypeByte(const QuicStreamFrame& frame, |
| 5985 | bool last_frame_in_packet) const { |
| 5986 | if (version_.transport_version == QUIC_VERSION_99) { |
| 5987 | return GetIetfStreamFrameTypeByte(frame, last_frame_in_packet); |
| 5988 | } |
| 5989 | uint8_t type_byte = 0; |
| 5990 | // Fin bit. |
| 5991 | type_byte |= frame.fin ? kQuicStreamFinMask : 0; |
| 5992 | |
| 5993 | // Data Length bit. |
| 5994 | type_byte <<= kQuicStreamDataLengthShift; |
| 5995 | type_byte |= last_frame_in_packet ? 0 : kQuicStreamDataLengthMask; |
| 5996 | |
| 5997 | // Offset 3 bits. |
| 5998 | type_byte <<= kQuicStreamShift; |
| 5999 | const size_t offset_len = |
| 6000 | GetStreamOffsetSize(version_.transport_version, frame.offset); |
| 6001 | if (offset_len > 0) { |
| 6002 | type_byte |= offset_len - 1; |
| 6003 | } |
| 6004 | |
| 6005 | // stream id 2 bits. |
| 6006 | type_byte <<= kQuicStreamIdShift; |
| 6007 | type_byte |= GetStreamIdSize(frame.stream_id) - 1; |
| 6008 | type_byte |= kQuicFrameTypeStreamMask; // Set Stream Frame Type to 1. |
| 6009 | |
| 6010 | return type_byte; |
| 6011 | } |
| 6012 | |
| 6013 | uint8_t QuicFramer::GetIetfStreamFrameTypeByte( |
| 6014 | const QuicStreamFrame& frame, |
| 6015 | bool last_frame_in_packet) const { |
| 6016 | DCHECK_EQ(QUIC_VERSION_99, version_.transport_version); |
| 6017 | uint8_t type_byte = IETF_STREAM; |
| 6018 | if (!last_frame_in_packet) { |
| 6019 | type_byte |= IETF_STREAM_FRAME_LEN_BIT; |
| 6020 | } |
| 6021 | if (frame.offset != 0) { |
| 6022 | type_byte |= IETF_STREAM_FRAME_OFF_BIT; |
| 6023 | } |
| 6024 | if (frame.fin) { |
| 6025 | type_byte |= IETF_STREAM_FRAME_FIN_BIT; |
| 6026 | } |
| 6027 | return type_byte; |
| 6028 | } |
| 6029 | |
| 6030 | void QuicFramer::InferPacketHeaderTypeFromVersion() { |
| 6031 | // This function should only be called when server connection negotiates the |
| 6032 | // version. |
| 6033 | DCHECK(perspective_ == Perspective::IS_SERVER && |
| 6034 | !infer_packet_header_type_from_version_); |
| 6035 | infer_packet_header_type_from_version_ = true; |
| 6036 | } |
| 6037 | |
QUICHE team | 10b22a1 | 2019-03-21 15:31:42 -0700 | [diff] [blame] | 6038 | void QuicFramer::EnableMultiplePacketNumberSpacesSupport() { |
| 6039 | if (supports_multiple_packet_number_spaces_) { |
| 6040 | QUIC_BUG << "Multiple packet number spaces has already been enabled"; |
| 6041 | return; |
| 6042 | } |
| 6043 | if (largest_packet_number_.IsInitialized()) { |
| 6044 | QUIC_BUG << "Try to enable multiple packet number spaces support after any " |
| 6045 | "packet has been received."; |
| 6046 | return; |
| 6047 | } |
| 6048 | |
| 6049 | supports_multiple_packet_number_spaces_ = true; |
| 6050 | } |
| 6051 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6052 | #undef ENDPOINT // undef for jumbo builds |
| 6053 | } // namespace quic |