42 static void DecodeTCPOptions(
Packet *p,
const uint8_t *pkt, uint16_t pktlen)
44 uint8_t tcp_opt_cnt = 0;
47 const TCPHdr *tcph = PacketGetTCP(p);
48 uint16_t plen = pktlen;
51 const uint8_t
type = *pkt;
66 const uint8_t olen = *(pkt+1);
71 if (
unlikely(olen > plen || olen < 2)) {
77 tcp_opts[tcp_opt_cnt].
len = olen;
78 tcp_opts[tcp_opt_cnt].
data = (olen > 2) ? (pkt+2) : NULL;
92 const uint8_t wscale = *(tcp_opts[tcp_opt_cnt].
data);
132 memcpy(&values, tcp_opts[tcp_opt_cnt].data,
sizeof(values));
144 !((olen - 2) % 8 == 0)) {
150 ptrdiff_t diff = tcp_opts[tcp_opt_cnt].
data - (uint8_t *)tcph;
161 !(((olen - 2) & 0x1) == 0))) {
175 if (olen == 4 || olen == 12) {
176 uint16_t magic =
SCNtohs(*(uint16_t *)tcp_opts[tcp_opt_cnt].data);
177 if (magic == 0xf989) {
217 static int DecodeTCPPacket(
225 TCPHdr *tcph = PacketSetTCP(p, pkt);
239 if (
likely(tcp_opt_len > 0)) {
246 p->
proto = IPPROTO_TCP;
248 p->
payload = (uint8_t *)pkt + hlen;
262 SCLogDebug(
"TCP sp: %u -> dp: %u - HLEN: %" PRIu32
" LEN: %" PRIu32
" %s%s%s%s%s%s", p->
sp,
289 static int TCPCalculateValidChecksumtest01(
void)
293 uint8_t raw_ipshdr[] = {
294 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
296 uint8_t raw_tcp[] = {
297 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
298 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
299 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
300 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
301 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 02};
303 csum = *( ((uint16_t *)raw_tcp) + 8);
305 FAIL_IF(TCPChecksum((uint16_t *)raw_ipshdr,
306 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) != 0);
310 static int TCPCalculateInvalidChecksumtest02(
void)
314 uint8_t raw_ipshdr[] = {
315 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
317 uint8_t raw_tcp[] = {
318 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
319 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
320 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
321 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
322 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 03};
324 csum = *( ((uint16_t *)raw_tcp) + 8);
326 FAIL_IF(TCPChecksum((uint16_t *) raw_ipshdr,
327 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) == 0);
331 static int TCPV6CalculateValidChecksumtest03(
void)
335 static uint8_t raw_ipv6[] = {
336 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
337 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
338 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
339 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
340 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
341 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
342 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
343 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
344 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
345 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
346 0xca, 0x5a, 0x00, 0x01, 0x69, 0x27};
348 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
350 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
351 (uint16_t *)(raw_ipv6 + 54), 32, csum) != 0);
355 static int TCPV6CalculateInvalidChecksumtest04(
void)
359 static uint8_t raw_ipv6[] = {
360 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
361 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
362 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
363 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
364 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
365 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
366 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
367 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
368 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
369 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
370 0xca, 0x5a, 0x00, 0x01, 0x69, 0x28};
372 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
374 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
375 (uint16_t *)(raw_ipv6 + 54), 32, csum) == 0);
380 static int TCPGetWscaleTest01(
void)
382 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
383 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
384 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
385 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
386 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x02};
394 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
414 static int TCPGetWscaleTest02(
void)
416 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
417 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
418 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
419 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
420 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x0f};
429 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
449 static int TCPGetWscaleTest03(
void)
451 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x59,
452 0xdd, 0xa3, 0x6f, 0xf8, 0x80, 0x10, 0x05, 0xb4,
453 0x7c, 0x70, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a,
454 0x00, 0x62, 0x88, 0x9e, 0x00, 0x00, 0x00, 0x00};
462 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
481 static int TCPGetSackTest01(
void)
483 static uint8_t raw_tcp[] = {
484 0x00, 0x50, 0x06, 0xa6, 0xfa, 0x87, 0x0b, 0xf5,
485 0xf1, 0x59, 0x02, 0xe0, 0xa0, 0x10, 0x3e, 0xbc,
486 0x1d, 0xe7, 0x00, 0x00, 0x01, 0x01, 0x05, 0x12,
487 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
488 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
489 static uint8_t raw_tcp_sack[] = {
490 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
491 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
500 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
516 const TCPHdr *tcph = PacketGetTCP(p);
520 FAIL_IF(memcmp(sackptr, raw_tcp_sack, 16) != 0);
533 TCPCalculateValidChecksumtest01);
535 TCPCalculateInvalidChecksumtest02);
537 TCPV6CalculateValidChecksumtest03);
539 TCPV6CalculateInvalidChecksumtest04);