42 #define SET_OPTS(dst, src) \
43 (dst).type = (src).type; \
44 (dst).len = (src).len; \
45 (dst).data = (src).data
47 static void DecodeTCPOptions(
Packet *p,
const uint8_t *pkt, uint16_t pktlen)
49 uint8_t tcp_opt_cnt = 0;
52 uint16_t plen = pktlen;
55 const uint8_t
type = *pkt;
70 const uint8_t olen = *(pkt+1);
75 if (
unlikely(olen > plen || olen < 2)) {
81 tcp_opts[tcp_opt_cnt].
len = olen;
82 tcp_opts[tcp_opt_cnt].
data = (olen > 2) ? (pkt+2) : NULL;
129 memcpy(&values, tcp_opts[tcp_opt_cnt].data,
sizeof(values));
141 !((olen - 2) % 8 == 0)))
155 !(((olen - 2) & 0x1) == 0))) {
169 if (olen == 4 || olen == 12) {
170 uint16_t magic =
SCNtohs(*(uint16_t *)tcp_opts[tcp_opt_cnt].data);
171 if (magic == 0xf989) {
219 p->
tcph = (TCPHdr *)pkt;
233 if (
likely(tcp_opt_len > 0)) {
240 p->
proto = IPPROTO_TCP;
242 p->
payload = (uint8_t *)pkt + hlen;
249 const uint8_t *pkt, uint16_t
len)
269 SCLogDebug(
"TCP sp: %" PRIu32
" -> dp: %" PRIu32
" - HLEN: %" PRIu32
" LEN: %" PRIu32
" %s%s%s%s%s%s",
285 static int TCPCalculateValidChecksumtest01(
void)
289 uint8_t raw_ipshdr[] = {
290 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
292 uint8_t raw_tcp[] = {
293 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
294 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
295 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
296 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
297 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 02};
299 csum = *( ((uint16_t *)raw_tcp) + 8);
301 FAIL_IF(TCPChecksum((uint16_t *)raw_ipshdr,
302 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) != 0);
306 static int TCPCalculateInvalidChecksumtest02(
void)
310 uint8_t raw_ipshdr[] = {
311 0x40, 0x8e, 0x7e, 0xb2, 0xc0, 0xa8, 0x01, 0x03};
313 uint8_t raw_tcp[] = {
314 0x00, 0x50, 0x8e, 0x16, 0x0d, 0x59, 0xcd, 0x3c,
315 0xcf, 0x0d, 0x21, 0x80, 0xa0, 0x12, 0x16, 0xa0,
316 0xfa, 0x03, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
317 0x04, 0x02, 0x08, 0x0a, 0x6e, 0x18, 0x78, 0x73,
318 0x01, 0x71, 0x74, 0xde, 0x01, 0x03, 0x03, 03};
320 csum = *( ((uint16_t *)raw_tcp) + 8);
322 FAIL_IF(TCPChecksum((uint16_t *) raw_ipshdr,
323 (uint16_t *)raw_tcp,
sizeof(raw_tcp), csum) == 0);
327 static int TCPV6CalculateValidChecksumtest03(
void)
331 static uint8_t raw_ipv6[] = {
332 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
333 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
334 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
335 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
336 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
337 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
338 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
339 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
340 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
341 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
342 0xca, 0x5a, 0x00, 0x01, 0x69, 0x27};
344 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
346 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
347 (uint16_t *)(raw_ipv6 + 54), 32, csum) != 0);
351 static int TCPV6CalculateInvalidChecksumtest04(
void)
355 static uint8_t raw_ipv6[] = {
356 0x00, 0x60, 0x97, 0x07, 0x69, 0xea, 0x00, 0x00,
357 0x86, 0x05, 0x80, 0xda, 0x86, 0xdd, 0x60, 0x00,
358 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x3f, 0xfe,
359 0x05, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x00,
360 0x86, 0xff, 0xfe, 0x05, 0x80, 0xda, 0x3f, 0xfe,
361 0x05, 0x01, 0x04, 0x10, 0x00, 0x00, 0x02, 0xc0,
362 0xdf, 0xff, 0xfe, 0x47, 0x03, 0x3e, 0x03, 0xfe,
363 0x00, 0x16, 0xd6, 0x76, 0xf5, 0x2d, 0x0c, 0x7a,
364 0x08, 0x77, 0x80, 0x10, 0x21, 0x5c, 0xc2, 0xf1,
365 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x00, 0x08,
366 0xca, 0x5a, 0x00, 0x01, 0x69, 0x28};
368 csum = *( ((uint16_t *)(raw_ipv6 + 70)));
370 FAIL_IF(TCPV6Checksum((uint16_t *)(raw_ipv6 + 14 + 8),
371 (uint16_t *)(raw_ipv6 + 54), 32, csum) == 0);
376 static int TCPGetWscaleTest01(
void)
379 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
380 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
381 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
382 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
383 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x02};
393 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
403 if (p->
tcph == NULL) {
404 printf(
"tcp packet decode failed: ");
410 printf(
"wscale %"PRIu8
", expected 2: ", wscale);
423 static int TCPGetWscaleTest02(
void)
426 static uint8_t raw_tcp[] = {0xda, 0xc1, 0x00, 0x50, 0xb6, 0x21, 0x7f, 0x58,
427 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02, 0x16, 0xd0,
428 0x8a, 0xaf, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4,
429 0x04, 0x02, 0x08, 0x0a, 0x00, 0x62, 0x88, 0x28,
430 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x0f};
440 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
449 if (p->
tcph == NULL) {
450 printf(
"tcp packet decode failed: ");
456 printf(
"wscale %"PRIu8
", expected 0: ", wscale);
469 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};
485 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
494 if (p->
tcph == NULL) {
495 printf(
"tcp packet decode failed: ");
501 printf(
"wscale %"PRIu8
", expected 0: ", wscale);
513 static int TCPGetSackTest01(
void)
516 static uint8_t raw_tcp[] = {
517 0x00, 0x50, 0x06, 0xa6, 0xfa, 0x87, 0x0b, 0xf5,
518 0xf1, 0x59, 0x02, 0xe0, 0xa0, 0x10, 0x3e, 0xbc,
519 0x1d, 0xe7, 0x00, 0x00, 0x01, 0x01, 0x05, 0x12,
520 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
521 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
522 static uint8_t raw_tcp_sack[] = {
523 0xf1, 0x59, 0x13, 0xfc, 0xf1, 0x59, 0x1f, 0x64,
524 0xf1, 0x59, 0x08, 0x94, 0xf1, 0x59, 0x0e, 0x48 };
534 memset(&ip4h, 0,
sizeof(
IPV4Hdr));
543 if (p->
tcph == NULL) {
544 printf(
"tcp packet decode failed: ");
549 printf(
"tcp packet sack not decoded: ");
560 if (sackptr == NULL) {
561 printf(
"no sack data: ");
565 if (memcmp(sackptr, raw_tcp_sack, 16) != 0) {
566 printf(
"malformed sack data: ");
583 TCPCalculateValidChecksumtest01);
585 TCPCalculateInvalidChecksumtest02);
587 TCPV6CalculateValidChecksumtest03);
589 TCPV6CalculateInvalidChecksumtest04);