lsquic_parse_iquic_common.c revision a0e1aeee
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * Parsing routines shared by all IETF QUIC versions. 4 */ 5 6#include <assert.h> 7#include <stddef.h> 8#include <stdint.h> 9#include <string.h> 10#include <sys/queue.h> 11#include <sys/types.h> 12 13#include <openssl/rand.h> 14 15#include "lsquic_types.h" 16#include "lsquic_int_types.h" 17#include "lsquic_packet_common.h" 18#include "lsquic_packet_in.h" 19#include "lsquic_parse_common.h" 20#include "lsquic_parse.h" 21#include "lsquic_version.h" 22#include "lsquic.h" 23#include "lsquic_logger.h" 24#include "lsquic_byteswap.h" 25#include "lsquic_varint.h" 26#include "lsquic_enc_sess.h" 27#include "lsquic_tokgen.h" 28#include "lsquic.h" 29#include "lsquic_mm.h" 30#include "lsquic_engine_public.h" 31 32 33/* [draft-ietf-quic-transport-17] Section-17.2 */ 34static const enum header_type bits2ht[4] = 35{ 36 [0] = HETY_INITIAL, 37 [1] = HETY_0RTT, 38 [2] = HETY_HANDSHAKE, 39 [3] = HETY_RETRY, 40}; 41 42 43int 44lsquic_Q046_parse_packet_in_long_begin (struct lsquic_packet_in *packet_in, 45 size_t length, int is_server, unsigned cid_len, 46 struct packin_parse_state *state) 47{ 48 const unsigned char *p = packet_in->pi_data; 49 const unsigned char *const end = p + length; 50 lsquic_ver_tag_t tag; 51 enum header_type header_type; 52 unsigned dcil, scil, packet_len; 53 int verneg; 54 unsigned char first_byte; 55 lsquic_packno_t packno; 56 57 if (length < 6) 58 return -1; 59 first_byte = *p++; 60 61 memcpy(&tag, p, 4); 62 p += 4; 63 verneg = 0 == tag; 64 if (!verneg) 65 header_type = bits2ht[ (first_byte >> 4) & 3 ]; 66 else 67 header_type = HETY_VERNEG; 68 69 packet_in->pi_header_type = header_type; 70 71 dcil = p[0] >> 4; 72 if (dcil) 73 dcil += 3; 74 scil = p[0] & 0xF; 75 if (scil) 76 scil += 3; 77 ++p; 78 79 /* Chromium comments state that the client sends packets with destination 80 * CID of 8 bytes and source CID of 0 bytes and the server does it the 81 * other way around. 82 */ 83 if (is_server) 84 { 85 if (!(dcil == cid_len && scil == 0)) 86 return -1; 87 } 88 else 89 if (!(dcil == 0 && scil == cid_len)) 90 return -1; 91 92 if (!verneg) 93 { 94 packet_in->pi_flags |= (first_byte & 3) << PIBIT_BITS_SHIFT; 95 packet_len = 1 + (first_byte & 3); 96 if (end - p < (ptrdiff_t) (dcil + scil + packet_len)) 97 return -1; 98 } 99 else 100 { 101 /* Need at least one version in the version array: add 4 */ 102 if (end - p < (ptrdiff_t) (dcil + scil + 4)) 103 return -1; 104#ifdef WIN32 105 /* Useless initialization: */ 106 packet_len = 0; 107#endif 108 } 109 110 memcpy(&packet_in->pi_dcid.idbuf, p, cid_len); 111 packet_in->pi_dcid.len = cid_len; 112 p += cid_len; 113 packet_in->pi_flags |= PI_CONN_ID; 114 115 if (!verneg) 116 { 117 READ_UINT(packno, 64, p, packet_len); 118 packet_in->pi_packno = packno; 119 p += packet_len; 120 packet_in->pi_quic_ver = 1; 121 if (is_server || HETY_0RTT != header_type) 122 packet_in->pi_nonce = 0; 123 else 124 { 125 packet_in->pi_nonce = p - packet_in->pi_data; 126 p += 32; 127 } 128 } 129 else 130 { 131 if (p >= end || (3 & (uintptr_t) (end - p))) 132 return -1; 133 packet_in->pi_quic_ver = p - packet_in->pi_data; 134 p = end; 135 } 136 137 packet_in->pi_header_sz = p - packet_in->pi_data; 138 packet_in->pi_frame_types = 0; 139 packet_in->pi_data_sz = length; 140 packet_in->pi_refcnt = 0; 141 packet_in->pi_received = 0; 142 143 return 0; 144} 145 146 147int 148lsquic_Q046_parse_packet_in_short_begin (lsquic_packet_in_t *packet_in, 149 size_t length, int is_server, unsigned cid_len, 150 struct packin_parse_state *state) 151{ 152 const unsigned char *p = packet_in->pi_data; 153 const unsigned char *const pend = packet_in->pi_data + length; 154 unsigned packet_len, header_len; 155 lsquic_packno_t packno; 156 157 if (*p & 0x40) /* Q046 and higher */ 158 packet_len = 1 + (*p & 3); 159 else 160 return -1; 161 162 if (is_server) 163 header_len = 1 + cid_len + packet_len; 164 else 165 header_len = 1 + packet_len; 166 167 if (pend - p < (ptrdiff_t) header_len) 168 return -1; 169 170 packet_in->pi_flags |= (*p & 3) << PIBIT_BITS_SHIFT; 171 ++p; 172 if (is_server) 173 { 174 memcpy(packet_in->pi_dcid.idbuf, packet_in->pi_data + 1, cid_len); 175 packet_in->pi_dcid.len = cid_len; 176 packet_in->pi_flags |= PI_CONN_ID; 177 p += cid_len; 178 } 179 180 READ_UINT(packno, 64, p, packet_len); 181 packet_in->pi_packno = packno; 182 p += packet_len; 183 184 packet_in->pi_header_type = HETY_NOT_SET; 185 packet_in->pi_quic_ver = 0; 186 packet_in->pi_nonce = 0; 187 packet_in->pi_header_sz = p - packet_in->pi_data; 188 packet_in->pi_frame_types = 0; 189 packet_in->pi_data_sz = length; 190 packet_in->pi_refcnt = 0; 191 packet_in->pi_received = 0; 192 193 return 0; 194} 195 196 197/* TODO: this only works Q044? XXX */ 198ssize_t 199lsquic_generate_iquic_reset (const lsquic_cid_t *cidp, unsigned char *buf, 200 size_t buf_sz) 201{ 202 size_t need; 203 uint64_t id; 204 205 need = 1 /* Type */ + 20 /* Random bytes */ + 16 /* Reset token */; 206 if (buf_sz < need) 207 return -1; 208 209 *buf = 0x30; 210 (void) RAND_pseudo_bytes(buf + 1, 20); 211 /* XXX code duplication here and lsquic_generate_reset_token(). Which 212 * should call which: parse function the crypto functions or the other 213 * way around? 214 */ 215 /* TODO test this */ 216 memcpy(&id, cidp->idbuf, GQUIC_CID_LEN); 217#if __BYTE_ORDER == __LITTLE_ENDIAN 218 id = bswap_64(id); 219#endif 220 memcpy(buf + 21, &id, sizeof(id)); 221 memset(buf + 21 + sizeof(id), 0, SRST_LENGTH - sizeof(id)); 222 return need; 223} 224 225 226/* This is a bare-bones version of lsquic_Q046_parse_packet_in_long_begin() 227 */ 228int 229lsquic_is_valid_iquic_hs_packet (const unsigned char *buf, size_t length, 230 lsquic_ver_tag_t *tagp) 231{ 232 const unsigned char *p = buf; 233 const unsigned char *const end = p + length; 234 lsquic_ver_tag_t tag; 235 unsigned dcil, scil, packet_len; 236 unsigned char first_byte; 237 const unsigned cid_len = 8; 238 239 if (length < 6) 240 return 0; 241 first_byte = *p++; 242 243 memcpy(&tag, p, 4); 244 p += 4; 245 if (0 == tag) 246 return 0; /* Client never sends version negotiation */ 247 248 dcil = p[0] >> 4; 249 if (dcil) 250 dcil += 3; 251 scil = p[0] & 0xF; 252 if (scil) 253 scil += 3; 254 ++p; 255 256 if (!(dcil == cid_len && scil == 0)) 257 return 0; 258 259 packet_len = first_byte & 3; 260 261 if (end - p >= (ptrdiff_t) (dcil + scil + packet_len)) 262 { 263 *tagp = tag; 264 return 1; 265 } 266 else 267 return 0; 268} 269 270 271const enum quic_frame_type lsquic_iquic_byte2type[0x100] = 272{ 273 [0x00] = QUIC_FRAME_PADDING, 274 [0x01] = QUIC_FRAME_PING, 275 [0x02] = QUIC_FRAME_ACK, 276 [0x03] = QUIC_FRAME_ACK, 277 [0x04] = QUIC_FRAME_RST_STREAM, 278 [0x05] = QUIC_FRAME_STOP_SENDING, 279 [0x06] = QUIC_FRAME_CRYPTO, 280 [0x07] = QUIC_FRAME_NEW_TOKEN, 281 [0x08] = QUIC_FRAME_STREAM, 282 [0x09] = QUIC_FRAME_STREAM, 283 [0x0A] = QUIC_FRAME_STREAM, 284 [0x0B] = QUIC_FRAME_STREAM, 285 [0x0C] = QUIC_FRAME_STREAM, 286 [0x0D] = QUIC_FRAME_STREAM, 287 [0x0E] = QUIC_FRAME_STREAM, 288 [0x0F] = QUIC_FRAME_STREAM, 289 [0x10] = QUIC_FRAME_MAX_DATA, 290 [0x11] = QUIC_FRAME_MAX_STREAM_DATA, 291 [0x12] = QUIC_FRAME_MAX_STREAMS, 292 [0x13] = QUIC_FRAME_MAX_STREAMS, 293 [0x14] = QUIC_FRAME_BLOCKED, 294 [0x15] = QUIC_FRAME_STREAM_BLOCKED, 295 [0x16] = QUIC_FRAME_STREAMS_BLOCKED, 296 [0x17] = QUIC_FRAME_STREAMS_BLOCKED, 297 [0x18] = QUIC_FRAME_NEW_CONNECTION_ID, 298 [0x19] = QUIC_FRAME_RETIRE_CONNECTION_ID, 299 [0x1A] = QUIC_FRAME_PATH_CHALLENGE, 300 [0x1B] = QUIC_FRAME_PATH_RESPONSE, 301 [0x1C] = QUIC_FRAME_CONNECTION_CLOSE, 302 [0x1D] = QUIC_FRAME_CONNECTION_CLOSE, 303 [0x1E] = QUIC_FRAME_INVALID, 304 [0x1F] = QUIC_FRAME_INVALID, 305 [0x20] = QUIC_FRAME_INVALID, 306 [0x21] = QUIC_FRAME_INVALID, 307 [0x22] = QUIC_FRAME_INVALID, 308 [0x23] = QUIC_FRAME_INVALID, 309 [0x24] = QUIC_FRAME_INVALID, 310 [0x25] = QUIC_FRAME_INVALID, 311 [0x26] = QUIC_FRAME_INVALID, 312 [0x27] = QUIC_FRAME_INVALID, 313 [0x28] = QUIC_FRAME_INVALID, 314 [0x29] = QUIC_FRAME_INVALID, 315 [0x2A] = QUIC_FRAME_INVALID, 316 [0x2B] = QUIC_FRAME_INVALID, 317 [0x2C] = QUIC_FRAME_INVALID, 318 [0x2D] = QUIC_FRAME_INVALID, 319 [0x2E] = QUIC_FRAME_INVALID, 320 [0x2F] = QUIC_FRAME_INVALID, 321 [0x30] = QUIC_FRAME_INVALID, 322 [0x31] = QUIC_FRAME_INVALID, 323 [0x32] = QUIC_FRAME_INVALID, 324 [0x33] = QUIC_FRAME_INVALID, 325 [0x34] = QUIC_FRAME_INVALID, 326 [0x35] = QUIC_FRAME_INVALID, 327 [0x36] = QUIC_FRAME_INVALID, 328 [0x37] = QUIC_FRAME_INVALID, 329 [0x38] = QUIC_FRAME_INVALID, 330 [0x39] = QUIC_FRAME_INVALID, 331 [0x3A] = QUIC_FRAME_INVALID, 332 [0x3B] = QUIC_FRAME_INVALID, 333 [0x3C] = QUIC_FRAME_INVALID, 334 [0x3D] = QUIC_FRAME_INVALID, 335 [0x3E] = QUIC_FRAME_INVALID, 336 [0x3F] = QUIC_FRAME_INVALID, 337 [0x40] = QUIC_FRAME_INVALID, 338 [0x41] = QUIC_FRAME_INVALID, 339 [0x42] = QUIC_FRAME_INVALID, 340 [0x43] = QUIC_FRAME_INVALID, 341 [0x44] = QUIC_FRAME_INVALID, 342 [0x45] = QUIC_FRAME_INVALID, 343 [0x46] = QUIC_FRAME_INVALID, 344 [0x47] = QUIC_FRAME_INVALID, 345 [0x48] = QUIC_FRAME_INVALID, 346 [0x49] = QUIC_FRAME_INVALID, 347 [0x4A] = QUIC_FRAME_INVALID, 348 [0x4B] = QUIC_FRAME_INVALID, 349 [0x4C] = QUIC_FRAME_INVALID, 350 [0x4D] = QUIC_FRAME_INVALID, 351 [0x4E] = QUIC_FRAME_INVALID, 352 [0x4F] = QUIC_FRAME_INVALID, 353 [0x50] = QUIC_FRAME_INVALID, 354 [0x51] = QUIC_FRAME_INVALID, 355 [0x52] = QUIC_FRAME_INVALID, 356 [0x53] = QUIC_FRAME_INVALID, 357 [0x54] = QUIC_FRAME_INVALID, 358 [0x55] = QUIC_FRAME_INVALID, 359 [0x56] = QUIC_FRAME_INVALID, 360 [0x57] = QUIC_FRAME_INVALID, 361 [0x58] = QUIC_FRAME_INVALID, 362 [0x59] = QUIC_FRAME_INVALID, 363 [0x5A] = QUIC_FRAME_INVALID, 364 [0x5B] = QUIC_FRAME_INVALID, 365 [0x5C] = QUIC_FRAME_INVALID, 366 [0x5D] = QUIC_FRAME_INVALID, 367 [0x5E] = QUIC_FRAME_INVALID, 368 [0x5F] = QUIC_FRAME_INVALID, 369 [0x60] = QUIC_FRAME_INVALID, 370 [0x61] = QUIC_FRAME_INVALID, 371 [0x62] = QUIC_FRAME_INVALID, 372 [0x63] = QUIC_FRAME_INVALID, 373 [0x64] = QUIC_FRAME_INVALID, 374 [0x65] = QUIC_FRAME_INVALID, 375 [0x66] = QUIC_FRAME_INVALID, 376 [0x67] = QUIC_FRAME_INVALID, 377 [0x68] = QUIC_FRAME_INVALID, 378 [0x69] = QUIC_FRAME_INVALID, 379 [0x6A] = QUIC_FRAME_INVALID, 380 [0x6B] = QUIC_FRAME_INVALID, 381 [0x6C] = QUIC_FRAME_INVALID, 382 [0x6D] = QUIC_FRAME_INVALID, 383 [0x6E] = QUIC_FRAME_INVALID, 384 [0x6F] = QUIC_FRAME_INVALID, 385 [0x70] = QUIC_FRAME_INVALID, 386 [0x71] = QUIC_FRAME_INVALID, 387 [0x72] = QUIC_FRAME_INVALID, 388 [0x73] = QUIC_FRAME_INVALID, 389 [0x74] = QUIC_FRAME_INVALID, 390 [0x75] = QUIC_FRAME_INVALID, 391 [0x76] = QUIC_FRAME_INVALID, 392 [0x77] = QUIC_FRAME_INVALID, 393 [0x78] = QUIC_FRAME_INVALID, 394 [0x79] = QUIC_FRAME_INVALID, 395 [0x7A] = QUIC_FRAME_INVALID, 396 [0x7B] = QUIC_FRAME_INVALID, 397 [0x7C] = QUIC_FRAME_INVALID, 398 [0x7D] = QUIC_FRAME_INVALID, 399 [0x7E] = QUIC_FRAME_INVALID, 400 [0x7F] = QUIC_FRAME_INVALID, 401 [0x80] = QUIC_FRAME_INVALID, 402 [0x81] = QUIC_FRAME_INVALID, 403 [0x82] = QUIC_FRAME_INVALID, 404 [0x83] = QUIC_FRAME_INVALID, 405 [0x84] = QUIC_FRAME_INVALID, 406 [0x85] = QUIC_FRAME_INVALID, 407 [0x86] = QUIC_FRAME_INVALID, 408 [0x87] = QUIC_FRAME_INVALID, 409 [0x88] = QUIC_FRAME_INVALID, 410 [0x89] = QUIC_FRAME_INVALID, 411 [0x8A] = QUIC_FRAME_INVALID, 412 [0x8B] = QUIC_FRAME_INVALID, 413 [0x8C] = QUIC_FRAME_INVALID, 414 [0x8D] = QUIC_FRAME_INVALID, 415 [0x8E] = QUIC_FRAME_INVALID, 416 [0x8F] = QUIC_FRAME_INVALID, 417 [0x90] = QUIC_FRAME_INVALID, 418 [0x91] = QUIC_FRAME_INVALID, 419 [0x92] = QUIC_FRAME_INVALID, 420 [0x93] = QUIC_FRAME_INVALID, 421 [0x94] = QUIC_FRAME_INVALID, 422 [0x95] = QUIC_FRAME_INVALID, 423 [0x96] = QUIC_FRAME_INVALID, 424 [0x97] = QUIC_FRAME_INVALID, 425 [0x98] = QUIC_FRAME_INVALID, 426 [0x99] = QUIC_FRAME_INVALID, 427 [0x9A] = QUIC_FRAME_INVALID, 428 [0x9B] = QUIC_FRAME_INVALID, 429 [0x9C] = QUIC_FRAME_INVALID, 430 [0x9D] = QUIC_FRAME_INVALID, 431 [0x9E] = QUIC_FRAME_INVALID, 432 [0x9F] = QUIC_FRAME_INVALID, 433 [0xA0] = QUIC_FRAME_INVALID, 434 [0xA1] = QUIC_FRAME_INVALID, 435 [0xA2] = QUIC_FRAME_INVALID, 436 [0xA3] = QUIC_FRAME_INVALID, 437 [0xA4] = QUIC_FRAME_INVALID, 438 [0xA5] = QUIC_FRAME_INVALID, 439 [0xA6] = QUIC_FRAME_INVALID, 440 [0xA7] = QUIC_FRAME_INVALID, 441 [0xA8] = QUIC_FRAME_INVALID, 442 [0xA9] = QUIC_FRAME_INVALID, 443 [0xAA] = QUIC_FRAME_INVALID, 444 [0xAB] = QUIC_FRAME_INVALID, 445 [0xAC] = QUIC_FRAME_INVALID, 446 [0xAD] = QUIC_FRAME_INVALID, 447 [0xAE] = QUIC_FRAME_INVALID, 448 [0xAF] = QUIC_FRAME_INVALID, 449 [0xB0] = QUIC_FRAME_INVALID, 450 [0xB1] = QUIC_FRAME_INVALID, 451 [0xB2] = QUIC_FRAME_INVALID, 452 [0xB3] = QUIC_FRAME_INVALID, 453 [0xB4] = QUIC_FRAME_INVALID, 454 [0xB5] = QUIC_FRAME_INVALID, 455 [0xB6] = QUIC_FRAME_INVALID, 456 [0xB7] = QUIC_FRAME_INVALID, 457 [0xB8] = QUIC_FRAME_INVALID, 458 [0xB9] = QUIC_FRAME_INVALID, 459 [0xBA] = QUIC_FRAME_INVALID, 460 [0xBB] = QUIC_FRAME_INVALID, 461 [0xBC] = QUIC_FRAME_INVALID, 462 [0xBD] = QUIC_FRAME_INVALID, 463 [0xBE] = QUIC_FRAME_INVALID, 464 [0xBF] = QUIC_FRAME_INVALID, 465 [0xC0] = QUIC_FRAME_INVALID, 466 [0xC1] = QUIC_FRAME_INVALID, 467 [0xC2] = QUIC_FRAME_INVALID, 468 [0xC3] = QUIC_FRAME_INVALID, 469 [0xC4] = QUIC_FRAME_INVALID, 470 [0xC5] = QUIC_FRAME_INVALID, 471 [0xC6] = QUIC_FRAME_INVALID, 472 [0xC7] = QUIC_FRAME_INVALID, 473 [0xC8] = QUIC_FRAME_INVALID, 474 [0xC9] = QUIC_FRAME_INVALID, 475 [0xCA] = QUIC_FRAME_INVALID, 476 [0xCB] = QUIC_FRAME_INVALID, 477 [0xCC] = QUIC_FRAME_INVALID, 478 [0xCD] = QUIC_FRAME_INVALID, 479 [0xCE] = QUIC_FRAME_INVALID, 480 [0xCF] = QUIC_FRAME_INVALID, 481 [0xD0] = QUIC_FRAME_INVALID, 482 [0xD1] = QUIC_FRAME_INVALID, 483 [0xD2] = QUIC_FRAME_INVALID, 484 [0xD3] = QUIC_FRAME_INVALID, 485 [0xD4] = QUIC_FRAME_INVALID, 486 [0xD5] = QUIC_FRAME_INVALID, 487 [0xD6] = QUIC_FRAME_INVALID, 488 [0xD7] = QUIC_FRAME_INVALID, 489 [0xD8] = QUIC_FRAME_INVALID, 490 [0xD9] = QUIC_FRAME_INVALID, 491 [0xDA] = QUIC_FRAME_INVALID, 492 [0xDB] = QUIC_FRAME_INVALID, 493 [0xDC] = QUIC_FRAME_INVALID, 494 [0xDD] = QUIC_FRAME_INVALID, 495 [0xDE] = QUIC_FRAME_INVALID, 496 [0xDF] = QUIC_FRAME_INVALID, 497 [0xE0] = QUIC_FRAME_INVALID, 498 [0xE1] = QUIC_FRAME_INVALID, 499 [0xE2] = QUIC_FRAME_INVALID, 500 [0xE3] = QUIC_FRAME_INVALID, 501 [0xE4] = QUIC_FRAME_INVALID, 502 [0xE5] = QUIC_FRAME_INVALID, 503 [0xE6] = QUIC_FRAME_INVALID, 504 [0xE7] = QUIC_FRAME_INVALID, 505 [0xE8] = QUIC_FRAME_INVALID, 506 [0xE9] = QUIC_FRAME_INVALID, 507 [0xEA] = QUIC_FRAME_INVALID, 508 [0xEB] = QUIC_FRAME_INVALID, 509 [0xEC] = QUIC_FRAME_INVALID, 510 [0xED] = QUIC_FRAME_INVALID, 511 [0xEE] = QUIC_FRAME_INVALID, 512 [0xEF] = QUIC_FRAME_INVALID, 513 [0xF0] = QUIC_FRAME_INVALID, 514 [0xF1] = QUIC_FRAME_INVALID, 515 [0xF2] = QUIC_FRAME_INVALID, 516 [0xF3] = QUIC_FRAME_INVALID, 517 [0xF4] = QUIC_FRAME_INVALID, 518 [0xF5] = QUIC_FRAME_INVALID, 519 [0xF6] = QUIC_FRAME_INVALID, 520 [0xF7] = QUIC_FRAME_INVALID, 521 [0xF8] = QUIC_FRAME_INVALID, 522 [0xF9] = QUIC_FRAME_INVALID, 523 [0xFA] = QUIC_FRAME_INVALID, 524 [0xFB] = QUIC_FRAME_INVALID, 525 [0xFC] = QUIC_FRAME_INVALID, 526 [0xFD] = QUIC_FRAME_INVALID, 527 [0xFE] = QUIC_FRAME_INVALID, 528 [0xFF] = QUIC_FRAME_INVALID, 529}; 530 531 532#if __GNUC__ 533# define popcount __builtin_popcount 534#else 535static int 536popcount (unsigned v) 537{ 538 int count, i; 539 for (i = 0, count = 0; i < sizeof(v) * 8; ++i) 540 if (v & (1 << i)) 541 ++count; 542 return count; 543} 544 545 546#endif 547 548 549int 550lsquic_Q046_gen_ver_nego_pkt (unsigned char *buf, size_t bufsz, 551 const lsquic_cid_t *scid, const lsquic_cid_t *dcid, unsigned versions, 552 uint8_t rand) 553{ 554 unsigned slen, dlen; 555 size_t need; 556 int r; 557 558 need = 1 /* Type */ + 4 /* Packet number */ + 1 /* SCIL */ 559 + scid->len + dcid->len + popcount(versions) * 4; 560 561 if (need > bufsz) 562 return -1; 563 564 *buf++ = 0x80 | 0x40 | (rand & 0xF); 565 memset(buf, 0, 4); 566 buf += 4; 567 568 /* From [draft-ietf-quic-transport-11], Section 4.3: 569 * 570 * The server MUST include the value from the Source Connection ID field 571 * of the packet it receives in the Destination Connection ID field. 572 * The value for Source Connection ID MUST be copied from the 573 * Destination Connection ID of the received packet, which is initially 574 * randomly selected by a client. Echoing both connection IDs gives 575 * clients some assurance that the server received the packet and that 576 * the Version Negotiation packet was not generated by an off-path 577 * attacker. 578 */ 579 580 dlen = dcid->len; 581 if (dlen) 582 dlen -= 3; 583 slen = scid->len; 584 if (slen) 585 slen -= 3; 586 *buf++ = (dlen << 4) | slen; 587 588 memcpy(buf, dcid->idbuf, dcid->len); 589 buf += dcid->len; 590 memcpy(buf, scid->idbuf, scid->len); 591 buf += scid->len; 592 593 r = lsquic_gen_ver_tags(buf, bufsz - 1 - 4 - 1 - dcid->len - scid->len, 594 versions); 595 if (r < 0) 596 return -1; 597 assert((unsigned) r == popcount(versions) * 4u); 598 599 return need; 600} 601 602 603