40 static inline uint16_t DecodeTCPGetU16(
const uint8_t *d)
42 return (uint16_t)(((uint16_t)d[0] << 8) | (uint16_t)d[1]);
45 static void DecodeTCPOptions(
Packet *
p,
const uint8_t *pkt, uint16_t pktlen)
47 uint8_t tcp_opt_cnt = 0;
50 const TCPHdr *tcph = PacketGetTCP(
p);
51 uint16_t plen = pktlen;
54 const uint8_t
type = *pkt;
69 const uint8_t olen = *(pkt+1);
74 if (
unlikely(olen > plen || olen < 2)) {
80 tcp_opts[tcp_opt_cnt].
len = olen;
90 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
95 const uint8_t wscale = *(tcp_opts[tcp_opt_cnt].
data);
113 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
118 p->
l4.
vars.
tcp.
mss = DecodeTCPGetU16(tcp_opts[tcp_opt_cnt].data);
126 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
138 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
143 memcpy(&values, tcp_opts[tcp_opt_cnt].data,
sizeof(values));
155 !((olen - 2) % 8 == 0)) {
158 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
162 ptrdiff_t diff = tcp_opts[tcp_opt_cnt].
data - (uint8_t *)tcph;
173 !(((olen - 2) & 0x1) == 0))) {
176 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
188 if (olen == 4 || olen == 12) {
189 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
190 uint16_t magic = DecodeTCPGetU16(tcp_opts[tcp_opt_cnt].data);
191 if (magic == 0xf989) {
208 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
219 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
226 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
229 tcp_opts[tcp_opt_cnt].
data = NULL;
240 static int DecodeTCPPacket(
248 TCPHdr *tcph = PacketSetTCP(
p, pkt);
262 if (
likely(tcp_opt_len > 0)) {
288 SCLogDebug(
"TCP sp: %u -> dp: %u - HLEN: %" PRIu32
" LEN: %" PRIu32
" %s%s%s%s%s%s",
p->
sp,
315 static int TCPCalculateValidChecksumtest01(
void)
319 uint8_t raw_ipshdr[] = {
320 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
322 uint8_t raw_tcp[] = {
323 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
324 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
325 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
326 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
327 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 02};
329 csum = *( ((uint16_t *)raw_tcp) + 8);
331 FAIL_IF(TCPChecksum((uint16_t *)raw_ipshdr,
332 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) != 0);
336 static int TCPCalculateInvalidChecksumtest02(
void)
340 uint8_t raw_ipshdr[] = {
341 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
343 uint8_t raw_tcp[] = {
344 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
345 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
346 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
347 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
348 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 03};
350 csum = *( ((uint16_t *)raw_tcp) + 8);
352 FAIL_IF(TCPChecksum((uint16_t *) raw_ipshdr,
353 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) == 0);
357 static int TCPV6CalculateValidChecksumtest03(
void)
361 static uint8_t raw_ipv6[] = {
362 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
363 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
364 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
365 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
366 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
367 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
368 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
369 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
370 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
371 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
372 0xca, 0x5a, 0x00, 0x01, 0x69, 0x27};
374 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
376 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
377 (uint16_t *)(raw_ipv6 + 54), 32, csum) != 0);
381 static int TCPV6CalculateInvalidChecksumtest04(
void)
385 static uint8_t raw_ipv6[] = {
386 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
387 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
388 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
389 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
390 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
391 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
392 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
393 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
394 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
395 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
396 0xca, 0x5a, 0x00, 0x01, 0x69, 0x28};
398 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
400 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
401 (uint16_t *)(raw_ipv6 + 54), 32, csum) == 0);
406 static int TCPGetWscaleTest01(
void)
408 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
409 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
410 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
411 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
412 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x02};
420 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
439 static int TCPGetWscaleTest02(
void)
441 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
442 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
443 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
444 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
445 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x0f};
454 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
473 static int TCPGetWscaleTest03(
void)
475 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x59,
476 0xdd, 0xa3, 0x6f, 0xf8, 0x80, 0x10, 0x05, 0xb4,
477 0x7c, 0x70, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a,
478 0x00, 0x62, 0x88, 0x9e, 0x00, 0x00, 0x00, 0x00};
486 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
504 static int TCPGetSackTest01(
void)
506 static uint8_t raw_tcp[] = {
507 0x00, 0x50, 0x06, 0xa6, 0xfa, 0x87, 0x0b, 0xf5,
508 0xf1, 0x59, 0x02, 0xe0, 0xa0, 0x10, 0x3e, 0xbc,
509 0x1d, 0xe7, 0x00, 0x00, 0x01, 0x01, 0x05, 0x12,
510 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
511 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
512 static uint8_t raw_tcp_sack[] = {
513 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
514 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
523 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
539 const TCPHdr *tcph = PacketGetTCP(
p);
543 FAIL_IF(memcmp(sackptr, raw_tcp_sack, 16) != 0);
555 TCPCalculateValidChecksumtest01);
557 TCPCalculateInvalidChecksumtest02);
559 TCPV6CalculateValidChecksumtest03);
561 TCPV6CalculateInvalidChecksumtest04);