42 static inline uint16_t DecodeTCPGetU16(
const uint8_t *d)
44 return (uint16_t)(((uint16_t)d[0] << 8) | (uint16_t)d[1]);
47 static void DecodeTCPOptions(
Packet *
p,
const uint8_t *pkt, uint16_t pktlen)
49 uint8_t tcp_opt_cnt = 0;
52 const TCPHdr *tcph = PacketGetTCP(
p);
53 uint16_t plen = pktlen;
56 const uint8_t
type = *pkt;
71 const uint8_t olen = *(pkt+1);
76 if (
unlikely(olen > plen || olen < 2)) {
82 tcp_opts[tcp_opt_cnt].
len = olen;
92 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
97 const uint8_t wscale = *(tcp_opts[tcp_opt_cnt].
data);
110 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
115 p->
l4.
vars.
tcp.
mss = DecodeTCPGetU16(tcp_opts[tcp_opt_cnt].data);
123 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
135 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
140 memcpy(&values, tcp_opts[tcp_opt_cnt].data,
sizeof(values));
152 !((olen - 2) % 8 == 0)) {
155 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
159 ptrdiff_t diff = tcp_opts[tcp_opt_cnt].
data - (uint8_t *)tcph;
170 !(((olen - 2) & 0x1) == 0))) {
173 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
185 if (olen == 4 || olen == 12) {
186 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
187 uint16_t magic = DecodeTCPGetU16(tcp_opts[tcp_opt_cnt].data);
188 if (magic == 0xf989) {
205 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
216 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
223 tcp_opts[tcp_opt_cnt].
data = (pkt + 2);
226 tcp_opts[tcp_opt_cnt].
data = NULL;
237 static int DecodeTCPPacket(
245 TCPHdr *tcph = PacketSetTCP(
p, pkt);
259 if (
likely(tcp_opt_len > 0)) {
285 SCLogDebug(
"TCP sp: %u -> dp: %u - HLEN: %" PRIu32
" LEN: %" PRIu32
" %s%s%s%s%s%s",
p->
sp,
312 static int TCPCalculateValidChecksumtest01(
void)
316 uint8_t raw_ipshdr[] = {
317 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
319 uint8_t raw_tcp[] = {
320 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
321 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
322 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
323 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
324 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 02};
326 csum = *( ((uint16_t *)raw_tcp) + 8);
328 FAIL_IF(TCPChecksum((uint16_t *)raw_ipshdr,
329 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) != 0);
333 static int TCPCalculateInvalidChecksumtest02(
void)
337 uint8_t raw_ipshdr[] = {
338 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
340 uint8_t raw_tcp[] = {
341 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
342 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
343 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
344 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
345 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 03};
347 csum = *( ((uint16_t *)raw_tcp) + 8);
349 FAIL_IF(TCPChecksum((uint16_t *) raw_ipshdr,
350 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) == 0);
354 static int TCPV6CalculateValidChecksumtest03(
void)
358 static uint8_t raw_ipv6[] = {
359 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
360 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
361 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
362 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
363 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
364 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
365 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
366 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
367 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
368 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
369 0xca, 0x5a, 0x00, 0x01, 0x69, 0x27};
371 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
373 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
374 (uint16_t *)(raw_ipv6 + 54), 32, csum) != 0);
378 static int TCPV6CalculateInvalidChecksumtest04(
void)
382 static uint8_t raw_ipv6[] = {
383 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
384 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
385 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
386 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
387 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
388 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
389 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
390 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
391 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
392 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
393 0xca, 0x5a, 0x00, 0x01, 0x69, 0x28};
395 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
397 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
398 (uint16_t *)(raw_ipv6 + 54), 32, csum) == 0);
403 static int TCPGetWscaleTest01(
void)
405 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
406 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
407 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
408 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
409 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x02};
417 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
436 static int TCPGetWscaleTest02(
void)
438 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
439 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
440 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
441 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
442 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x0f};
451 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
470 static int TCPGetWscaleTest03(
void)
472 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x59,
473 0xdd, 0xa3, 0x6f, 0xf8, 0x80, 0x10, 0x05, 0xb4,
474 0x7c, 0x70, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a,
475 0x00, 0x62, 0x88, 0x9e, 0x00, 0x00, 0x00, 0x00};
483 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
501 static int TCPGetSackTest01(
void)
503 static uint8_t raw_tcp[] = {
504 0x00, 0x50, 0x06, 0xa6, 0xfa, 0x87, 0x0b, 0xf5,
505 0xf1, 0x59, 0x02, 0xe0, 0xa0, 0x10, 0x3e, 0xbc,
506 0x1d, 0xe7, 0x00, 0x00, 0x01, 0x01, 0x05, 0x12,
507 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
508 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
509 static uint8_t raw_tcp_sack[] = {
510 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
511 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
520 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
536 const TCPHdr *tcph = PacketGetTCP(
p);
540 FAIL_IF(memcmp(sackptr, raw_tcp_sack, 16) != 0);
552 TCPCalculateValidChecksumtest01);
554 TCPCalculateInvalidChecksumtest02);
556 TCPV6CalculateValidChecksumtest03);
558 TCPV6CalculateInvalidChecksumtest04);