1// Copyright 2012 Google, Inc. All rights reserved. 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the LICENSE file in the root of the source 5// tree. 6 7package reassembly 8 9import ( 10 "bytes" 11 "encoding/hex" 12 "fmt" 13 "math/rand" 14 "net" 15 "reflect" 16 "runtime" 17 "testing" 18 "time" 19 20 "github.com/google/gopacket" 21 "github.com/google/gopacket/layers" 22) 23 24var netFlow gopacket.Flow 25 26var testDebug = false 27 28func init() { 29 netFlow, _ = gopacket.FlowFromEndpoints( 30 layers.NewIPEndpoint(net.IP{1, 2, 3, 4}), 31 layers.NewIPEndpoint(net.IP{5, 6, 7, 8})) 32} 33 34type Reassembly struct { 35 Bytes []byte 36 Start bool 37 End bool 38 Skip int 39} 40 41type testSequence struct { 42 in layers.TCP 43 want []Reassembly 44} 45 46/* For benchmark: do nothing */ 47type testFactoryBench struct { 48} 49 50func (t *testFactoryBench) New(a, b gopacket.Flow, tcp *layers.TCP, ac AssemblerContext) Stream { 51 return t 52} 53func (t *testFactoryBench) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir TCPFlowDirection, seq Sequence, start *bool, ac AssemblerContext) bool { 54 return true 55} 56func (t *testFactoryBench) ReassembledSG(sg ScatterGather, ac AssemblerContext) { 57} 58func (t *testFactoryBench) ReassemblyComplete(ac AssemblerContext) bool { 59 return true 60} 61 62/* For tests: append bytes */ 63type testFactory struct { 64 reassembly []Reassembly 65} 66 67func (t *testFactory) New(a, b gopacket.Flow, tcp *layers.TCP, ac AssemblerContext) Stream { 68 return t 69} 70func (t *testFactory) Reassembled(r []Reassembly) { 71 t.reassembly = r 72 for i := 0; i < len(r); i++ { 73 //t.reassembly[i].Seen = time.Time{} 74 } 75} 76func (t *testFactory) ReassembledSG(sg ScatterGather, ac AssemblerContext) { 77 _, start, end, skip := sg.Info() 78 l, _ := sg.Lengths() 79 t.reassembly = append(t.reassembly, Reassembly{ 80 Bytes: sg.Fetch(l), 81 Skip: skip, 82 Start: start, 83 End: end, 84 }) 85} 86 87func (t *testFactory) ReassemblyComplete(ac AssemblerContext) bool { 88 return true 89} 90 91func (t *testFactory) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir TCPFlowDirection, seq Sequence, start *bool, ac AssemblerContext) bool { 92 return true 93} 94 95/* For memory checks: counts bytes */ 96type testMemoryFactory struct { 97 bytes int 98} 99 100func (tf *testMemoryFactory) New(a, b gopacket.Flow, tcp *layers.TCP, ac AssemblerContext) Stream { 101 return tf 102} 103func (tf *testMemoryFactory) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir TCPFlowDirection, seq Sequence, start *bool, ac AssemblerContext) bool { 104 return true 105} 106func (tf *testMemoryFactory) ReassembledSG(sg ScatterGather, ac AssemblerContext) { 107 bytes, _ := sg.Lengths() 108 tf.bytes += bytes 109} 110func (tf *testMemoryFactory) ReassemblyComplete(ac AssemblerContext) bool { 111 return true 112} 113 114/* 115 * Tests 116 */ 117 118func test(t *testing.T, s []testSequence) { 119 fact := &testFactory{} 120 p := NewStreamPool(fact) 121 a := NewAssembler(p) 122 a.MaxBufferedPagesPerConnection = 4 123 for i, test := range s { 124 fact.reassembly = []Reassembly{} 125 if testDebug { 126 fmt.Printf("#### test: #%d: sending:%s\n", i, hex.EncodeToString(test.in.BaseLayer.Payload)) 127 } 128 a.Assemble(netFlow, &test.in) 129 final := []Reassembly{} 130 if len(test.want) > 0 { 131 final = append(final, Reassembly{}) 132 for _, w := range test.want { 133 final[0].Bytes = append(final[0].Bytes, w.Bytes...) 134 if w.End { 135 final[0].End = true 136 } 137 if w.Start { 138 final[0].Start = true 139 } 140 if w.Skip != 0 { 141 final[0].Skip = w.Skip 142 } 143 } 144 } 145 if !reflect.DeepEqual(fact.reassembly, final) { 146 t.Fatalf("test %v:\nwant: %v\n got: %v\n", i, final, fact.reassembly) 147 } 148 if testDebug { 149 fmt.Printf("test %v passing...(%v)\n", i, final) 150 } 151 } 152} 153 154func TestReorder(t *testing.T) { 155 test(t, []testSequence{ 156 { 157 in: layers.TCP{ 158 SrcPort: 1, 159 DstPort: 2, 160 Seq: 1001, 161 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 162 }, 163 want: []Reassembly{}, 164 }, 165 { 166 in: layers.TCP{ 167 SrcPort: 1, 168 DstPort: 2, 169 Seq: 1004, 170 BaseLayer: layers.BaseLayer{Payload: []byte{4, 5, 6}}, 171 }, 172 want: []Reassembly{}, 173 }, 174 { 175 in: layers.TCP{ 176 SrcPort: 1, 177 DstPort: 2, 178 Seq: 1010, 179 BaseLayer: layers.BaseLayer{Payload: []byte{10, 11, 12}}, 180 }, 181 want: []Reassembly{}, 182 }, 183 { 184 in: layers.TCP{ 185 SrcPort: 1, 186 DstPort: 2, 187 Seq: 1007, 188 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9}}, 189 }, 190 want: []Reassembly{ 191 Reassembly{ 192 Skip: -1, 193 Bytes: []byte{1, 2, 3}, 194 }, 195 Reassembly{ 196 Bytes: []byte{4, 5, 6}, 197 }, 198 Reassembly{ 199 Bytes: []byte{7, 8, 9}, 200 }, 201 Reassembly{ 202 Bytes: []byte{10, 11, 12}, 203 }, 204 }, 205 }, 206 { 207 in: layers.TCP{ 208 SrcPort: 1, 209 DstPort: 2, 210 Seq: 1016, 211 BaseLayer: layers.BaseLayer{Payload: []byte{2, 2, 3}}, 212 }, 213 want: []Reassembly{}, 214 }, 215 { 216 in: layers.TCP{ 217 SrcPort: 1, 218 DstPort: 2, 219 Seq: 1019, 220 BaseLayer: layers.BaseLayer{Payload: []byte{3, 2, 3}}, 221 }, 222 want: []Reassembly{}, 223 }, 224 { 225 in: layers.TCP{ 226 SrcPort: 1, 227 DstPort: 2, 228 Seq: 1013, 229 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 230 }, 231 want: []Reassembly{ 232 Reassembly{ 233 Bytes: []byte{1, 2, 3}, 234 }, 235 Reassembly{ 236 Bytes: []byte{2, 2, 3}, 237 }, 238 Reassembly{ 239 Bytes: []byte{3, 2, 3}, 240 }, 241 }, 242 }, 243 }) 244} 245 246func TestMaxPerSkip(t *testing.T) { 247 test(t, []testSequence{ 248 { 249 in: layers.TCP{ 250 SrcPort: 1, 251 DstPort: 2, 252 Seq: 1000, 253 SYN: true, 254 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 255 }, 256 want: []Reassembly{ 257 Reassembly{ 258 Start: true, 259 Bytes: []byte{1, 2, 3}, 260 }, 261 }, 262 }, 263 { 264 in: layers.TCP{ 265 SrcPort: 1, 266 DstPort: 2, 267 Seq: 1007, 268 BaseLayer: layers.BaseLayer{Payload: []byte{3, 2, 3}}, 269 }, 270 want: []Reassembly{}, 271 }, 272 { 273 in: layers.TCP{ 274 SrcPort: 1, 275 DstPort: 2, 276 Seq: 1010, 277 BaseLayer: layers.BaseLayer{Payload: []byte{4, 2, 3}}, 278 }, 279 want: []Reassembly{}, 280 }, 281 { 282 in: layers.TCP{ 283 SrcPort: 1, 284 DstPort: 2, 285 Seq: 1013, 286 BaseLayer: layers.BaseLayer{Payload: []byte{5, 2, 3}}, 287 }, 288 want: []Reassembly{}, 289 }, 290 { 291 in: layers.TCP{ 292 SrcPort: 1, 293 DstPort: 2, 294 Seq: 1016, 295 BaseLayer: layers.BaseLayer{Payload: []byte{6, 2, 3}}, 296 }, 297 want: []Reassembly{ 298 Reassembly{ 299 Skip: 3, 300 Bytes: []byte{3, 2, 3}, 301 }, 302 Reassembly{ 303 Bytes: []byte{4, 2, 3}, 304 }, 305 Reassembly{ 306 Bytes: []byte{5, 2, 3}, 307 }, 308 Reassembly{ 309 Bytes: []byte{6, 2, 3}, 310 }, 311 }, 312 }, 313 }) 314} 315 316func TestReorderFast(t *testing.T) { 317 test(t, []testSequence{ 318 { 319 in: layers.TCP{ 320 SrcPort: 1, 321 DstPort: 2, 322 SYN: true, 323 Seq: 1000, 324 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 325 }, 326 want: []Reassembly{ 327 Reassembly{ 328 Start: true, 329 Bytes: []byte{1, 2, 3}, 330 }, 331 }, 332 }, 333 { 334 in: layers.TCP{ 335 SrcPort: 1, 336 DstPort: 2, 337 Seq: 1007, 338 BaseLayer: layers.BaseLayer{Payload: []byte{3, 2, 3}}, 339 }, 340 want: []Reassembly{}, 341 }, 342 { 343 in: layers.TCP{ 344 SrcPort: 1, 345 DstPort: 2, 346 Seq: 1004, 347 BaseLayer: layers.BaseLayer{Payload: []byte{2, 2, 3}}, 348 }, 349 want: []Reassembly{ 350 Reassembly{ 351 Bytes: []byte{2, 2, 3}, 352 }, 353 Reassembly{ 354 Bytes: []byte{3, 2, 3}, 355 }, 356 }, 357 }, 358 }) 359} 360 361func TestOverlap(t *testing.T) { 362 test(t, []testSequence{ 363 { 364 in: layers.TCP{ 365 SrcPort: 1, 366 DstPort: 2, 367 SYN: true, 368 Seq: 1000, 369 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 370 }, 371 want: []Reassembly{ 372 Reassembly{ 373 Start: true, 374 Bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 375 }, 376 }, 377 }, 378 { 379 in: layers.TCP{ 380 SrcPort: 1, 381 DstPort: 2, 382 Seq: 1007, 383 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 0, 1, 2, 3, 4, 5}}, 384 }, 385 want: []Reassembly{ 386 Reassembly{ 387 Bytes: []byte{1, 2, 3, 4, 5}, 388 }, 389 }, 390 }, 391 { 392 in: layers.TCP{ 393 SrcPort: 1, 394 DstPort: 2, 395 Seq: 1010, 396 BaseLayer: layers.BaseLayer{Payload: []byte{0, 1, 2, 3, 4, 5, 6, 7}}, 397 }, 398 want: []Reassembly{ 399 Reassembly{ 400 Bytes: []byte{6, 7}, 401 }, 402 }, 403 }, 404 }) 405} 406 407func TestBufferedOverlap1(t *testing.T) { 408 test(t, []testSequence{ 409 { 410 in: layers.TCP{ 411 SrcPort: 1, 412 DstPort: 2, 413 Seq: 1007, 414 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 0, 1, 2, 3, 4, 5}}, 415 }, 416 want: []Reassembly{}, 417 }, 418 { 419 in: layers.TCP{ 420 SrcPort: 1, 421 DstPort: 2, 422 Seq: 1010, 423 BaseLayer: layers.BaseLayer{Payload: []byte{0, 1, 2, 3, 4, 5, 6, 7}}, 424 }, 425 want: []Reassembly{}, 426 }, 427 { 428 in: layers.TCP{ 429 SrcPort: 1, 430 DstPort: 2, 431 SYN: true, 432 Seq: 1000, 433 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 434 }, 435 want: []Reassembly{ 436 Reassembly{ 437 Start: true, 438 Bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 439 }, 440 Reassembly{ 441 Bytes: []byte{1, 2, 3, 4, 5}, 442 }, 443 Reassembly{ 444 Bytes: []byte{6, 7}, 445 }, 446 }, 447 }, 448 }) 449} 450 451func TestBufferedOverlapCase6(t *testing.T) { 452 test(t, []testSequence{ 453 { 454 in: layers.TCP{ 455 SrcPort: 1, 456 DstPort: 2, 457 Seq: 1007, 458 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 0, 1, 2, 3, 4, 5}}, 459 }, 460 want: []Reassembly{}, 461 }, 462 { 463 in: layers.TCP{ 464 SrcPort: 1, 465 DstPort: 2, 466 Seq: 1007, 467 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 10, 11, 12, 13, 14}}, 468 }, 469 want: []Reassembly{}, 470 }, 471 { 472 in: layers.TCP{ 473 SrcPort: 1, 474 DstPort: 2, 475 SYN: true, 476 Seq: 1000, 477 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 478 }, 479 want: []Reassembly{ 480 Reassembly{ 481 Start: true, 482 Bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 483 }, 484 Reassembly{ 485 Bytes: []byte{11, 12, 13, 14, 5}, 486 }, 487 }, 488 }, 489 }) 490} 491 492func TestBufferedOverlapExisting(t *testing.T) { 493 test(t, []testSequence{ 494 { 495 in: layers.TCP{ 496 SrcPort: 1, 497 DstPort: 2, 498 Seq: 1000, 499 SYN: true, 500 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7}}, 501 }, 502 want: []Reassembly{ 503 Reassembly{ 504 Start: true, 505 Bytes: []byte{1, 2, 3, 4, 5, 6, 7}, 506 }, 507 }, 508 }, 509 { 510 in: layers.TCP{ 511 SrcPort: 1, 512 DstPort: 2, 513 Seq: 1005, 514 BaseLayer: layers.BaseLayer{Payload: []byte{5, 6, 7, 8, 9, 10}}, 515 }, 516 want: []Reassembly{ 517 Reassembly{ 518 Bytes: []byte{8, 9, 10}, 519 }, 520 }, 521 }, 522 }) 523} 524 525func TestBufferedOverlapReemit(t *testing.T) { 526 test(t, []testSequence{ 527 { 528 in: layers.TCP{ 529 SrcPort: 1, 530 DstPort: 2, 531 Seq: 1000, 532 SYN: true, 533 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7}}, 534 }, 535 want: []Reassembly{ 536 Reassembly{ 537 Start: true, 538 Bytes: []byte{1, 2, 3, 4, 5, 6, 7}, 539 }, 540 }, 541 }, 542 { 543 in: layers.TCP{ 544 SrcPort: 1, 545 DstPort: 2, 546 Seq: 1003, 547 BaseLayer: layers.BaseLayer{Payload: []byte{3, 4, 5}}, 548 }, 549 want: []Reassembly{}, 550 }, 551 }) 552} 553 554func TestReorderRetransmission2(t *testing.T) { 555 test(t, []testSequence{ 556 { 557 in: layers.TCP{ 558 SrcPort: 1, 559 DstPort: 2, 560 Seq: 1001, 561 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 562 }, 563 want: []Reassembly{}, 564 }, 565 { 566 in: layers.TCP{ 567 SrcPort: 1, 568 DstPort: 2, 569 Seq: 1007, 570 BaseLayer: layers.BaseLayer{Payload: []byte{2, 2, 3}}, 571 }, 572 want: []Reassembly{}, 573 }, 574 { 575 in: layers.TCP{ 576 SrcPort: 1, 577 DstPort: 2, 578 Seq: 1007, 579 BaseLayer: layers.BaseLayer{Payload: []byte{2, 2, 3}}, 580 }, 581 want: []Reassembly{}, 582 }, 583 { 584 in: layers.TCP{ 585 SrcPort: 1, 586 DstPort: 2, 587 Seq: 1010, 588 BaseLayer: layers.BaseLayer{Payload: []byte{10, 11}}, 589 }, 590 want: []Reassembly{}, 591 }, 592 { 593 in: layers.TCP{ 594 SrcPort: 1, 595 DstPort: 2, 596 Seq: 1004, 597 BaseLayer: layers.BaseLayer{Payload: []byte{6, 6, 6, 2, 2}}, 598 }, 599 want: []Reassembly{ 600 Reassembly{ 601 Skip: -1, 602 Bytes: []byte{1, 2, 3}, 603 }, 604 Reassembly{ 605 Bytes: []byte{6, 6, 6}, 606 }, 607 Reassembly{ 608 Bytes: []byte{2, 2, 3}, 609 }, 610 Reassembly{ 611 Bytes: []byte{10, 11}, 612 }, 613 }, 614 }, 615 }) 616} 617 618func TestOverrun1(t *testing.T) { 619 test(t, []testSequence{ 620 { 621 in: layers.TCP{ 622 SrcPort: 1, 623 DstPort: 2, 624 SYN: true, 625 Seq: 0xFFFFFFFF, 626 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 627 }, 628 want: []Reassembly{ 629 Reassembly{ 630 Start: true, 631 Bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 632 }, 633 }, 634 }, 635 { 636 in: layers.TCP{ 637 SrcPort: 1, 638 DstPort: 2, 639 Seq: 10, 640 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4}}, 641 }, 642 want: []Reassembly{ 643 Reassembly{ 644 Bytes: []byte{1, 2, 3, 4}, 645 }, 646 }, 647 }, 648 }) 649} 650 651func TestOverrun2(t *testing.T) { 652 test(t, []testSequence{ 653 { 654 in: layers.TCP{ 655 SrcPort: 1, 656 DstPort: 2, 657 Seq: 10, 658 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4}}, 659 }, 660 want: []Reassembly{}, 661 }, 662 { 663 in: layers.TCP{ 664 SrcPort: 1, 665 DstPort: 2, 666 SYN: true, 667 Seq: 0xFFFFFFFF, 668 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 669 }, 670 want: []Reassembly{ 671 Reassembly{ 672 Start: true, 673 Bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 674 }, 675 Reassembly{ 676 Bytes: []byte{1, 2, 3, 4}, 677 }, 678 }, 679 }, 680 }) 681} 682 683func TestCacheLargePacket(t *testing.T) { 684 data := make([]byte, pageBytes*3) 685 test(t, []testSequence{ 686 { 687 in: layers.TCP{ 688 SrcPort: 1, 689 DstPort: 2, 690 Seq: 1001, 691 BaseLayer: layers.BaseLayer{Payload: data}, 692 }, 693 want: []Reassembly{}, 694 }, 695 { 696 in: layers.TCP{ 697 SrcPort: 1, 698 DstPort: 2, 699 Seq: 1000, 700 SYN: true, 701 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 702 }, 703 want: []Reassembly{ 704 Reassembly{ 705 Start: true, 706 Bytes: []byte{}, 707 }, 708 Reassembly{ 709 Bytes: data[:pageBytes], 710 }, 711 Reassembly{ 712 Bytes: data[pageBytes : pageBytes*2], 713 }, 714 Reassembly{ 715 Bytes: data[pageBytes*2 : pageBytes*3], 716 }, 717 }, 718 }, 719 }) 720} 721 722func testFlush(t *testing.T, s []testSequence, delay time.Duration, flushInterval time.Duration) { 723 fact := &testFactory{} 724 p := NewStreamPool(fact) 725 a := NewAssembler(p) 726 a.MaxBufferedPagesPerConnection = 10 727 port := layers.TCPPort(0) 728 729 simTime := time.Unix(0, 0) 730 731 for i, test := range s { 732 fact.reassembly = []Reassembly{} 733 if testDebug { 734 fmt.Printf("#### test: #%d: sending:%s\n", i, hex.EncodeToString(test.in.BaseLayer.Payload)) 735 } 736 737 flow := netFlow 738 if port == 0 { 739 port = test.in.SrcPort 740 } 741 if port != test.in.SrcPort { 742 flow = flow.Reverse() 743 } 744 ctx := assemblerSimpleContext(gopacket.CaptureInfo{Timestamp: simTime}) 745 a.AssembleWithContext(flow, &test.in, &ctx) 746 simTime = simTime.Add(delay) 747 a.FlushCloseOlderThan(simTime.Add(-1 * flushInterval)) 748 749 final := []Reassembly{} 750 if len(test.want) > 0 { 751 final = append(final, Reassembly{}) 752 for _, w := range test.want { 753 final[0].Bytes = append(final[0].Bytes, w.Bytes...) 754 if w.End { 755 final[0].End = true 756 } 757 if w.Start { 758 final[0].Start = true 759 } 760 if w.Skip != 0 { 761 final[0].Skip = w.Skip 762 } 763 } 764 } 765 766 if !reflect.DeepEqual(fact.reassembly, final) { 767 t.Errorf("test %v:\nwant: %v\n got: %v\n", i, final, fact.reassembly) 768 } 769 770 if testDebug { 771 fmt.Printf("test %v passing...(%v)\n", i, final) 772 } 773 } 774} 775 776func TestFlush(t *testing.T) { 777 for _, test := range []struct { 778 seq []testSequence 779 delay, flushOlderThan time.Duration 780 }{ 781 { 782 seq: []testSequence{ 783 { 784 in: layers.TCP{ 785 SrcPort: 1, 786 DstPort: 2, 787 Seq: 1001, 788 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 789 }, 790 want: []Reassembly{ 791 // flushed after flush interval. 792 Reassembly{ 793 Skip: -1, 794 Bytes: []byte{1, 2, 3}, 795 }, 796 }, 797 }, 798 { 799 in: layers.TCP{ 800 SrcPort: 1, 801 DstPort: 2, 802 Seq: 1010, 803 BaseLayer: layers.BaseLayer{Payload: []byte{4, 5, 6, 7}}, 804 }, 805 want: []Reassembly{ 806 // flushed after flush interval. 807 Reassembly{ 808 Skip: -1, 809 Bytes: []byte{4, 5, 6, 7}, 810 }, 811 }, 812 }, 813 }, 814 delay: time.Millisecond * 50, 815 flushOlderThan: time.Millisecond * 40, 816 }, 817 { 818 // two way stream. 819 seq: []testSequence{ 820 { 821 in: layers.TCP{ 822 SrcPort: 1, 823 DstPort: 2, 824 Seq: 1001, 825 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 826 }, 827 want: []Reassembly{}, 828 }, 829 { 830 in: layers.TCP{ 831 SrcPort: 2, 832 DstPort: 1, 833 Seq: 890, 834 BaseLayer: layers.BaseLayer{Payload: []byte{11, 22, 33}}, 835 }, 836 want: []Reassembly{ 837 // First half is flushed after flush interval. 838 Reassembly{ 839 Skip: -1, 840 Bytes: []byte{1, 2, 3}, 841 }, 842 }, 843 }, 844 { 845 in: layers.TCP{ 846 SrcPort: 2, 847 DstPort: 1, 848 Seq: 893, 849 BaseLayer: layers.BaseLayer{Payload: []byte{44, 55, 66, 77}}, 850 }, 851 want: []Reassembly{ 852 // continues data is flushed. 853 Reassembly{ 854 Skip: -1, 855 Bytes: []byte{11, 22, 33, 44, 55, 66, 77}, 856 }, 857 }, 858 }, 859 { 860 in: layers.TCP{ 861 SrcPort: 1, 862 DstPort: 2, 863 Seq: 1004, 864 BaseLayer: layers.BaseLayer{Payload: []byte{8, 9}}, 865 }, 866 want: []Reassembly{ 867 Reassembly{ 868 // Should be flushed because is continues. 869 Bytes: []byte{8, 9}, 870 }, 871 }, 872 }, 873 }, 874 delay: time.Millisecond * 50, 875 flushOlderThan: time.Millisecond * 99, 876 }, 877 // a late RST packet 878 { 879 seq: []testSequence{ 880 { 881 in: layers.TCP{ 882 SrcPort: 1, 883 DstPort: 2, 884 Seq: 1005, 885 BaseLayer: layers.BaseLayer{Payload: []byte{5, 6, 7}}, 886 }, 887 // gets queued 888 want: []Reassembly{}, 889 }, 890 { 891 in: layers.TCP{ 892 SrcPort: 1, 893 DstPort: 2, 894 RST: true, 895 Seq: 1001, 896 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3}}, 897 }, 898 // gets queued just before the first packet 899 // and should close its half-connection (RST) during next flush 900 want: []Reassembly{}, 901 }, 902 { 903 // triggers flush/close 904 in: layers.TCP{ 905 SrcPort: 1, 906 DstPort: 2, 907 Seq: 1010, 908 BaseLayer: layers.BaseLayer{Payload: []byte{10, 11, 12}}, 909 }, 910 want: []Reassembly{ 911 Reassembly{ 912 Skip: -1, 913 End: true, 914 Bytes: []byte{1, 2, 3}, 915 }, 916 }, 917 }, 918 }, 919 delay: time.Millisecond * 40, 920 flushOlderThan: time.Millisecond * 50, 921 }, 922 } { 923 testFlush(t, test.seq, test.delay, test.flushOlderThan) 924 } 925} 926 927/* 928 * Keep 929 */ 930type testKeepFactory struct { 931 keep int 932 bytes []byte 933 skipped int 934 t *testing.T 935} 936 937func (tkf *testKeepFactory) New(a, b gopacket.Flow, tcp *layers.TCP, ac AssemblerContext) Stream { 938 return tkf 939} 940func (tkf *testKeepFactory) ReassembledSG(sg ScatterGather, ac AssemblerContext) { 941 l, _ := sg.Lengths() 942 _, _, _, tkf.skipped = sg.Info() 943 tkf.bytes = sg.Fetch(l) 944 sg.KeepFrom(tkf.keep) 945} 946func (tkf *testKeepFactory) ReassemblyComplete(ac AssemblerContext) bool { 947 return true 948} 949 950func (tkf *testKeepFactory) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir TCPFlowDirection, seq Sequence, start *bool, ac AssemblerContext) bool { 951 return true 952} 953 954type testKeepSequence struct { 955 tcp layers.TCP 956 keep int 957 want []byte 958 skipped int 959 flush bool 960} 961 962func testKeep(t *testing.T, s []testKeepSequence) { 963 fact := &testKeepFactory{t: t} 964 p := NewStreamPool(fact) 965 a := NewAssembler(p) 966 a.MaxBufferedPagesPerConnection = 4 967 port := layers.TCPPort(0) 968 for i, test := range s { 969 // Fake some values according to ports 970 flow := netFlow 971 dir := TCPDirClientToServer 972 if port == 0 { 973 port = test.tcp.SrcPort 974 } 975 if port != test.tcp.SrcPort { 976 dir = dir.Reverse() 977 flow = flow.Reverse() 978 } 979 test.tcp.SetInternalPortsForTesting() 980 fact.keep = test.keep 981 fact.bytes = []byte{} 982 if testDebug { 983 fmt.Printf("#### testKeep: #%d: sending:%s\n", i, hex.EncodeToString(test.tcp.BaseLayer.Payload)) 984 } 985 a.Assemble(flow, &test.tcp) 986 if !reflect.DeepEqual(fact.bytes, test.want) { 987 t.Fatalf("#%d: invalid bytes: got %v, expected %v", i, fact.bytes, test.want) 988 } 989 if fact.skipped != test.skipped { 990 t.Fatalf("#%d: expecting %d skipped bytes, got %d", i, test.skipped, fact.skipped) 991 } 992 if testDebug { 993 fmt.Printf("#### testKeep: #%d: bytes: %s\n", i, hex.EncodeToString(fact.bytes)) 994 } 995 996 if test.flush { 997 a.FlushAll() 998 } 999 } 1000} 1001 1002func TestKeepSimpleOnBoundary(t *testing.T) { 1003 testKeep(t, []testKeepSequence{ 1004 { 1005 tcp: layers.TCP{ 1006 SrcPort: 1, 1007 DstPort: 2, 1008 SYN: true, 1009 Seq: 1000, 1010 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1011 }, 1012 keep: 0, 1013 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 1014 }, 1015 { 1016 tcp: layers.TCP{ 1017 SrcPort: 1, 1018 DstPort: 2, 1019 Seq: 1007, 1020 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 0, 1, 2, 3, 4, 5}}, 1021 }, 1022 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5}, 1023 }, 1024 }) 1025} 1026 1027func TestKeepSimpleNotBoundaryLive(t *testing.T) { 1028 testKeep(t, []testKeepSequence{ 1029 { 1030 tcp: layers.TCP{ 1031 SrcPort: 1, 1032 DstPort: 2, 1033 SYN: true, 1034 Seq: 1000, 1035 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1036 }, 1037 keep: 1, 1038 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 1039 }, 1040 { 1041 tcp: layers.TCP{ 1042 SrcPort: 1, 1043 DstPort: 2, 1044 Seq: 1007, 1045 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 0, 1, 2, 3, 4, 5}}, 1046 }, 1047 want: []byte{2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5}, 1048 }, 1049 }) 1050} 1051 1052func TestKeepSimpleNotBoundaryAlreadyKept(t *testing.T) { 1053 testKeep(t, []testKeepSequence{ 1054 { 1055 tcp: layers.TCP{ 1056 SrcPort: 1, 1057 DstPort: 2, 1058 SYN: true, 1059 Seq: 1000, 1060 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0x10}}, 1061 }, 1062 keep: 0, // 1→10 1063 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0x10}, 1064 }, 1065 { 1066 tcp: layers.TCP{ 1067 SrcPort: 1, 1068 DstPort: 2, 1069 Seq: 1007, 1070 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15}}, 1071 }, 1072 keep: 11, // 12→15 1073 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15}, 1074 }, 1075 { 1076 tcp: layers.TCP{ 1077 SrcPort: 1, 1078 DstPort: 2, 1079 Seq: 1016, 1080 BaseLayer: layers.BaseLayer{Payload: []byte{0x16, 0x17, 0x18}}, 1081 }, 1082 want: []byte{0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}, 1083 }, 1084 }) 1085} 1086 1087func TestKeepLonger(t *testing.T) { 1088 testKeep(t, []testKeepSequence{ 1089 { 1090 tcp: layers.TCP{ 1091 SrcPort: 1, 1092 DstPort: 2, 1093 SYN: true, 1094 Seq: 1000, 1095 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, 1096 }, 1097 keep: 0, 1098 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 1099 }, 1100 { 1101 tcp: layers.TCP{ 1102 SrcPort: 1, 1103 DstPort: 2, 1104 Seq: 1007, 1105 BaseLayer: layers.BaseLayer{Payload: []byte{7, 8, 9, 10, 11, 12, 13, 14, 15}}, 1106 }, 1107 keep: 0, 1108 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, 1109 }, 1110 { 1111 tcp: layers.TCP{ 1112 SrcPort: 1, 1113 DstPort: 2, 1114 Seq: 1010, 1115 BaseLayer: layers.BaseLayer{Payload: []byte{10, 11, 12, 13, 14, 15, 16, 17}}, 1116 }, 1117 want: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, 1118 }, 1119 }) 1120} 1121 1122func TestKeepWithFlush(t *testing.T) { 1123 testKeep(t, []testKeepSequence{ 1124 { 1125 tcp: layers.TCP{ 1126 SrcPort: 1, 1127 DstPort: 2, 1128 SYN: true, 1129 Seq: 1000, 1130 BaseLayer: layers.BaseLayer{Payload: []byte{1}}, 1131 }, 1132 keep: 1, 1133 want: []byte{1}, 1134 }, 1135 { 1136 tcp: layers.TCP{ 1137 SrcPort: 1, 1138 DstPort: 2, 1139 Seq: 1003, 1140 BaseLayer: layers.BaseLayer{Payload: []byte{3}}, 1141 }, 1142 keep: 0, 1143 want: []byte{}, 1144 }, 1145 { 1146 tcp: layers.TCP{ 1147 SrcPort: 1, 1148 DstPort: 2, 1149 Seq: 1004, 1150 BaseLayer: layers.BaseLayer{Payload: []byte{4}}, 1151 }, 1152 keep: 0, 1153 want: []byte{}, 1154 }, 1155 { 1156 tcp: layers.TCP{ 1157 SrcPort: 1, 1158 DstPort: 2, 1159 Seq: 1006, 1160 BaseLayer: layers.BaseLayer{Payload: []byte{6}}, 1161 }, 1162 keep: 0, 1163 want: []byte{}, 1164 }, 1165 // Exceeding 4 pages: flushing first continuous pages 1166 { 1167 tcp: layers.TCP{ 1168 SrcPort: 1, 1169 DstPort: 2, 1170 Seq: 1008, 1171 BaseLayer: layers.BaseLayer{Payload: []byte{8}}, 1172 }, 1173 keep: 0, 1174 skipped: 1, 1175 want: []byte{3, 4}, 1176 }, 1177 { 1178 tcp: layers.TCP{ 1179 SrcPort: 1, 1180 DstPort: 2, 1181 Seq: 1010, 1182 BaseLayer: layers.BaseLayer{Payload: []byte{10}}, 1183 }, 1184 keep: 0, 1185 skipped: 1, 1186 want: []byte{6}, 1187 }, 1188 { 1189 tcp: layers.TCP{ 1190 SrcPort: 1, 1191 DstPort: 2, 1192 Seq: 1012, 1193 BaseLayer: layers.BaseLayer{Payload: []byte{12}}, 1194 }, 1195 keep: 0, 1196 skipped: 1, 1197 want: []byte{8}, 1198 }, 1199 }) 1200} 1201 1202func TestKeepWithOutOfOrderPacketAndManualFlush(t *testing.T) { 1203 makePayload := func(length int) []byte { 1204 data := make([]byte, length) 1205 rand.Read(data) 1206 return data 1207 } 1208 1209 // The first packet is received out of order. It contains `pageBytes + 1` 1210 // number of bytes, so it spans 2 pages. 1211 // The second packet carries a single byte before the first packet, and we 1212 // request to keep `pageBytes` bytes. Then trigger a flush. 1213 // Prior to a fix, this would result in an slice bounds out of range panic 1214 // when the code tries to incorrectly skip the leading bytes on the second 1215 // page of the first packet. 1216 testKeep(t, []testKeepSequence{ 1217 { 1218 tcp: layers.TCP{ 1219 SrcPort: 1, 1220 DstPort: 2, 1221 Seq: 1001, 1222 BaseLayer: layers.BaseLayer{Payload: makePayload(pageBytes + 1)}, 1223 }, 1224 want: []byte{}, 1225 }, 1226 { 1227 tcp: layers.TCP{ 1228 SrcPort: 1, 1229 DstPort: 2, 1230 Seq: 1000, 1231 BaseLayer: layers.BaseLayer{Payload: []byte{1}}, 1232 }, 1233 keep: pageBytes, 1234 want: []byte{}, 1235 flush: true, 1236 }, 1237 }) 1238} 1239 1240/* 1241 * FSM tests 1242 */ 1243/* For FSM: bump nb on accepted packet */ 1244type testFSMFactory struct { 1245 nb int 1246 fsm TCPSimpleFSM 1247} 1248 1249func (t *testFSMFactory) New(a, b gopacket.Flow, tcp *layers.TCP, ac AssemblerContext) Stream { 1250 return t 1251} 1252func (t *testFSMFactory) ReassembledSG(sg ScatterGather, ac AssemblerContext) { 1253} 1254func (t *testFSMFactory) ReassemblyComplete(ac AssemblerContext) bool { 1255 return false 1256} 1257 1258func (t *testFSMFactory) Accept(tcp *layers.TCP, ci gopacket.CaptureInfo, dir TCPFlowDirection, seq Sequence, start *bool, ac AssemblerContext) bool { 1259 ok := t.fsm.CheckState(tcp, dir) 1260 if ok { 1261 t.nb++ 1262 } 1263 return ok 1264} 1265 1266type testFSMSequence struct { 1267 tcp layers.TCP 1268 ci gopacket.CaptureInfo 1269 nb int 1270} 1271 1272func (seq *testFSMSequence) GetCaptureInfo() gopacket.CaptureInfo { 1273 return seq.ci 1274} 1275 1276func testFSM(t *testing.T, s []testFSMSequence) { 1277 fact := &testFSMFactory{} 1278 p := NewStreamPool(fact) 1279 a := NewAssembler(p) 1280 //a.MaxBufferedPagesPerConnection = 4 1281 fact.nb = 0 1282 port := layers.TCPPort(0) 1283 for i, test := range s { 1284 // Fake some values according to ports 1285 flow := netFlow 1286 dir := TCPDirClientToServer 1287 if port == 0 { 1288 port = test.tcp.SrcPort 1289 } 1290 if port != test.tcp.SrcPort { 1291 dir = dir.Reverse() 1292 flow = flow.Reverse() 1293 } 1294 test.tcp.SetInternalPortsForTesting() 1295 a.AssembleWithContext(flow, &test.tcp, &test) 1296 if fact.nb != test.nb { 1297 t.Fatalf("#%d: packet rejected: got %d, expected %d", i, fact.nb, test.nb) 1298 } 1299 } 1300} 1301 1302func TestFSMnormalFlow(t *testing.T) { 1303 testFSM(t, []testFSMSequence{ 1304 { 1305 tcp: layers.TCP{ 1306 SYN: true, 1307 SrcPort: 54842, 1308 DstPort: 53, 1309 Seq: 374511116, 1310 Ack: 0, 1311 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1312 }, 1313 ci: gopacket.CaptureInfo{ 1314 Timestamp: time.Unix(1432538521, 566690000), 1315 }, 1316 nb: 1, 1317 }, 1318 { 1319 tcp: layers.TCP{ 1320 SYN: true, 1321 ACK: true, 1322 SrcPort: 53, 1323 DstPort: 54842, 1324 Seq: 3465787765, 1325 Ack: 374511117, 1326 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1327 }, 1328 ci: gopacket.CaptureInfo{ 1329 Timestamp: time.Unix(1432538521, 590332000), 1330 }, 1331 nb: 2, 1332 }, 1333 { 1334 tcp: layers.TCP{ 1335 ACK: true, 1336 SrcPort: 54842, 1337 DstPort: 53, 1338 Seq: 374511117, 1339 Ack: 3465787766, 1340 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1341 }, 1342 ci: gopacket.CaptureInfo{ 1343 Timestamp: time.Unix(1432538521, 590346000), 1344 }, 1345 nb: 3, 1346 }, 1347 { 1348 tcp: layers.TCP{ 1349 ACK: true, 1350 SrcPort: 54842, 1351 DstPort: 53, 1352 Seq: 374511117, 1353 Ack: 3465787766, 1354 BaseLayer: layers.BaseLayer{Payload: []byte{0, 31, 104, 196, 0, 32, 0, 1, 0, 0, 0, 0, 0, 1, 2, 85, 83, 0, 0, 6, 0, 1, 0, 0, 41, 16, 0, 0, 0, 128, 0, 0, 0}}, 1355 }, 1356 ci: gopacket.CaptureInfo{ 1357 Timestamp: time.Unix(1432538521, 590387000), 1358 }, 1359 nb: 4, 1360 }, 1361 { 1362 tcp: layers.TCP{ 1363 ACK: true, 1364 SrcPort: 53, 1365 DstPort: 54842, 1366 Seq: 3465787766, 1367 Ack: 374511150, 1368 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1369 }, 1370 ci: gopacket.CaptureInfo{ 1371 Timestamp: time.Unix(1432538521, 613687000), 1372 }, 1373 nb: 5, 1374 }, 1375 { 1376 tcp: layers.TCP{ 1377 ACK: true, 1378 SrcPort: 53, 1379 DstPort: 54842, 1380 Seq: 3465787766, 1381 Ack: 374511150, 1382 BaseLayer: layers.BaseLayer{Payload: []byte{8, 133, 104, 196, 132, 0, 0, 1, 0, 2, 0, 7, 0, 19, 2, 85, 83, 0, 0, 6, 0, 1, 2, 117, 115, 0, 0, 6, 0, 1, 0, 0, 3, 132, 0, 54, 1, 97, 5, 99, 99, 116, 108, 100, 192, 20, 10, 104, 111, 115, 116, 109, 97, 115, 116, 101, 114, 7, 110, 101, 117, 115, 116, 97, 114, 3, 98, 105, 122, 0, 120, 18, 40, 205, 0, 0, 3, 132, 0, 0, 3, 132, 0, 9, 58, 128, 0, 1, 81, 128, 192, 20, 0, 46, 0, 1, 0, 0, 3, 132, 0, 150, 0, 6, 5, 1, 0, 0, 3, 132, 85, 138, 90, 146, 85, 98, 191, 130, 27, 78, 2, 117, 115, 0, 69, 13, 35, 189, 141, 225, 107, 238, 108, 182, 207, 44, 105, 31, 212, 103, 32, 93, 217, 108, 20, 231, 188, 28, 241, 237, 104, 182, 117, 121, 195, 112, 64, 96, 237, 248, 6, 181, 186, 96, 60, 6, 18, 29, 188, 96, 201, 140, 251, 61, 71, 177, 108, 156, 9, 83, 125, 172, 188, 75, 81, 67, 218, 55, 93, 131, 243, 15, 190, 75, 4, 165, 226, 124, 49, 67, 142, 131, 239, 240, 76, 225, 10, 242, 68, 88, 240, 200, 27, 97, 102, 73, 92, 73, 133, 170, 175, 198, 99, 109, 90, 16, 162, 101, 95, 96, 102, 250, 91, 74, 80, 3, 87, 167, 50, 230, 9, 213, 7, 222, 197, 87, 183, 190, 148, 247, 207, 204, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 10, 1, 102, 5, 99, 99, 116, 108, 100, 192, 12, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 97, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 98, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 99, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 101, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 107, 193, 8, 192, 118, 0, 46, 0, 1, 0, 7, 233, 0, 0, 150, 0, 2, 5, 1, 0, 7, 233, 0, 85, 127, 33, 92, 85, 87, 134, 98, 27, 78, 2, 117, 115, 0, 19, 227, 175, 75, 88, 245, 164, 158, 150, 198, 57, 253, 150, 179, 161, 52, 24, 56, 229, 176, 175, 40, 45, 232, 188, 171, 131, 197, 107, 125, 218, 192, 78, 221, 146, 33, 114, 55, 43, 12, 131, 213, 51, 98, 37, 2, 102, 161, 232, 115, 177, 210, 51, 169, 215, 133, 56, 190, 91, 75, 8, 222, 231, 202, 139, 28, 187, 249, 72, 21, 23, 56, 63, 72, 126, 142, 242, 195, 242, 64, 208, 134, 100, 157, 197, 159, 43, 148, 20, 70, 117, 152, 159, 35, 200, 220, 49, 234, 173, 210, 91, 34, 210, 192, 7, 197, 112, 117, 208, 234, 42, 49, 133, 237, 197, 14, 244, 149, 191, 142, 36, 252, 42, 48, 182, 189, 9, 68, 1, 65, 5, 67, 67, 84, 76, 68, 193, 126, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 124, 70, 1, 66, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 125, 70, 194, 26, 0, 28, 0, 1, 0, 0, 28, 32, 0, 16, 32, 1, 5, 3, 209, 174, 255, 255, 255, 255, 255, 255, 255, 255, 255, 126, 1, 67, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 127, 70, 1, 69, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 126, 70, 1, 70, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 209, 173, 58, 70, 194, 108, 0, 28, 0, 1, 0, 0, 28, 32, 0, 16, 32, 1, 5, 0, 54, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 1, 75, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 128, 70, 194, 154, 0, 28, 0, 1, 0, 0, 28, 32, 0, 16, 32, 1, 5, 3, 226, 57, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 194, 2, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 112, 230, 49, 85, 73, 83, 2, 27, 78, 2, 117, 115, 0, 82, 36, 11, 141, 74, 85, 70, 98, 179, 63, 173, 83, 8, 70, 155, 41, 102, 166, 140, 62, 71, 178, 130, 38, 171, 200, 180, 68, 2, 215, 45, 6, 43, 59, 171, 146, 223, 215, 9, 77, 5, 104, 167, 42, 237, 170, 30, 114, 205, 129, 59, 225, 152, 224, 79, 1, 65, 68, 208, 153, 121, 237, 199, 87, 2, 251, 100, 105, 59, 24, 73, 226, 169, 121, 250, 91, 41, 124, 14, 23, 135, 52, 2, 86, 72, 224, 100, 135, 70, 216, 16, 107, 84, 59, 13, 168, 58, 187, 54, 98, 230, 167, 246, 42, 46, 156, 206, 238, 120, 199, 25, 144, 98, 249, 70, 162, 34, 43, 145, 114, 186, 233, 47, 42, 75, 95, 152, 235, 194, 26, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 112, 190, 140, 85, 73, 36, 78, 27, 78, 2, 117, 115, 0, 160, 95, 100, 37, 167, 82, 93, 165, 126, 247, 147, 173, 238, 154, 206, 174, 96, 175, 209, 7, 8, 169, 171, 223, 29, 201, 161, 177, 98, 54, 94, 62, 70, 127, 142, 109, 206, 42, 179, 109, 156, 160, 156, 20, 59, 24, 147, 164, 13, 121, 192, 84, 157, 26, 56, 177, 151, 210, 7, 197, 229, 110, 60, 58, 224, 42, 77, 5, 59, 80, 216, 221, 248, 19, 66, 102, 74, 199, 238, 120, 231, 201, 187, 29, 11, 46, 195, 164, 8, 221, 128, 25, 205, 42, 247, 152, 112, 176, 14, 117, 150, 223, 245, 32, 212, 107, 4, 245, 27, 126, 224, 216, 0, 89, 106, 238, 185, 206, 44, 56, 204, 175, 7, 139, 233, 228, 127, 175, 194, 26, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 28, 5, 3, 0, 0, 28, 32, 85, 108, 217, 174, 85, 69, 70, 242, 27, 78, 2, 117, 115, 0, 172, 117, 89, 89, 73, 249, 245, 211, 100, 127, 48, 135, 224, 97, 172, 146, 128, 30, 190, 72, 199, 170, 97, 179, 136, 109, 86, 110, 235, 214, 47, 50, 115, 11, 226, 168, 56, 198, 24, 212, 205, 207, 2, 116, 104, 112, 99, 234, 236, 44, 70, 19, 19, 215, 127, 200, 162, 215, 142, 45, 135, 91, 219, 217, 86, 231, 154, 87, 222, 161, 32, 66, 196, 55, 117, 20, 186, 9, 134, 252, 249, 219, 9, 196, 128, 8, 222, 201, 131, 210, 182, 232, 142, 72, 160, 171, 95, 231, 232, 156, 28, 34, 54, 94, 73, 183, 38, 160, 123, 175, 157, 21, 163, 8, 214, 155, 172, 237, 169, 28, 15, 138, 105, 107, 251, 109, 131, 240, 194, 72, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 112, 190, 140, 85, 73, 36, 78, 27, 78, 2, 117, 115, 0, 77, 207, 197, 130, 236, 138, 192, 241, 225, 114, 8, 22, 76, 54, 43, 121, 42, 44, 9, 92, 56, 253, 224, 179, 191, 131, 40, 176, 94, 61, 33, 12, 43, 82, 156, 236, 211, 29, 187, 100, 220, 243, 24, 134, 42, 204, 46, 161, 214, 91, 68, 119, 40, 252, 53, 54, 146, 136, 196, 168, 204, 195, 131, 110, 6, 73, 16, 161, 86, 35, 150, 153, 162, 185, 227, 65, 228, 160, 203, 42, 250, 121, 14, 42, 115, 221, 232, 96, 99, 164, 230, 29, 195, 149, 85, 206, 41, 1, 252, 77, 188, 88, 8, 182, 37, 249, 6, 158, 6, 244, 158, 254, 141, 203, 6, 158, 198, 103, 130, 98, 123, 34, 245, 44, 126, 77, 24, 187, 194, 90, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 108, 194, 203, 85, 69, 51, 125, 27, 78, 2, 117, 115, 0, 86, 26, 187, 56, 252, 194, 199, 140, 229, 133, 186, 187, 20, 174, 26, 48, 212, 129, 10, 20, 167, 179, 53, 72, 176, 92, 153, 48, 146, 15, 163, 182, 80, 138, 181, 135, 98, 129, 17, 66, 55, 184, 76, 225, 72, 104, 7, 221, 40, 71, 41, 202, 246, 154, 166, 199, 74, 175, 146, 54, 25, 56, 115, 243}}, 1383 }, 1384 ci: gopacket.CaptureInfo{ 1385 Timestamp: time.Unix(1432538521, 621198000), 1386 }, 1387 nb: 6, 1388 }, 1389 { 1390 tcp: layers.TCP{ 1391 ACK: true, 1392 SrcPort: 54842, 1393 DstPort: 53, 1394 Seq: 374511150, 1395 Ack: 3465789226, 1396 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1397 }, 1398 ci: gopacket.CaptureInfo{ 1399 Timestamp: time.Unix(1432538521, 621220000), 1400 }, 1401 nb: 7, 1402 }, 1403 { 1404 tcp: layers.TCP{ 1405 ACK: true, 1406 SrcPort: 53, 1407 DstPort: 54842, 1408 Seq: 3465789226, 1409 Ack: 374511150, 1410 BaseLayer: layers.BaseLayer{Payload: []byte{153, 141, 101, 187, 110, 15, 63, 42, 81, 100, 95, 68, 241, 85, 160, 227, 3, 1, 12, 80, 166, 1, 98, 2, 44, 98, 63, 203, 70, 164, 99, 195, 23, 152, 223, 253, 208, 10, 12, 19, 66, 121, 9, 158, 205, 96, 218, 0, 80, 70, 58, 95, 41, 124, 216, 13, 122, 135, 102, 200, 181, 233, 129, 174, 194, 108, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 108, 223, 157, 85, 69, 74, 55, 27, 78, 2, 117, 115, 0, 149, 71, 215, 149, 16, 165, 115, 229, 141, 136, 187, 158, 88, 225, 131, 231, 182, 218, 235, 27, 48, 65, 244, 77, 186, 135, 72, 18, 87, 52, 180, 128, 130, 67, 75, 173, 160, 243, 104, 178, 103, 117, 96, 209, 36, 51, 108, 47, 232, 214, 254, 15, 208, 182, 218, 174, 248, 237, 88, 150, 35, 190, 239, 249, 171, 151, 9, 236, 2, 252, 255, 13, 79, 190, 147, 36, 161, 210, 202, 80, 209, 136, 167, 180, 186, 68, 246, 249, 48, 123, 46, 11, 132, 103, 132, 207, 186, 68, 110, 133, 142, 109, 194, 19, 122, 57, 203, 217, 120, 93, 67, 168, 91, 252, 87, 38, 33, 228, 229, 162, 190, 170, 23, 188, 89, 15, 241, 71, 194, 108, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 28, 5, 3, 0, 0, 28, 32, 85, 108, 217, 174, 85, 69, 70, 242, 27, 78, 2, 117, 115, 0, 206, 97, 120, 37, 255, 252, 7, 156, 162, 192, 43, 84, 105, 94, 125, 55, 13, 247, 234, 9, 25, 100, 246, 25, 77, 168, 199, 208, 187, 209, 164, 123, 234, 138, 238, 15, 86, 45, 163, 108, 162, 117, 247, 128, 3, 187, 100, 185, 193, 191, 134, 86, 161, 254, 236, 99, 66, 66, 35, 173, 91, 243, 175, 3, 175, 94, 79, 68, 246, 109, 200, 154, 209, 185, 11, 210, 50, 147, 136, 213, 158, 81, 111, 17, 149, 239, 110, 114, 25, 234, 247, 158, 233, 33, 36, 181, 66, 84, 189, 37, 207, 58, 9, 171, 143, 66, 69, 137, 192, 6, 187, 59, 16, 51, 80, 56, 89, 170, 12, 195, 69, 133, 188, 110, 171, 17, 17, 213, 194, 154, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 112, 190, 140, 85, 73, 36, 78, 27, 78, 2, 117, 115, 0, 123, 36, 154, 4, 158, 41, 96, 252, 116, 114, 16, 137, 28, 177, 206, 33, 192, 88, 89, 1, 69, 252, 206, 88, 89, 152, 210, 179, 248, 44, 202, 239, 95, 131, 126, 147, 249, 93, 57, 166, 215, 184, 211, 164, 196, 71, 170, 3, 25, 18, 177, 214, 94, 147, 181, 148, 197, 11, 171, 219, 107, 48, 105, 81, 239, 110, 249, 140, 68, 127, 193, 146, 176, 161, 246, 108, 75, 141, 205, 211, 73, 247, 125, 205, 120, 156, 82, 55, 130, 250, 26, 15, 44, 214, 91, 115, 11, 103, 22, 83, 184, 96, 107, 138, 2, 127, 168, 191, 92, 102, 137, 161, 63, 225, 134, 17, 178, 242, 11, 43, 8, 30, 164, 28, 140, 195, 83, 121, 194, 154, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 28, 5, 3, 0, 0, 28, 32, 85, 112, 190, 140, 85, 73, 36, 78, 27, 78, 2, 117, 115, 0, 189, 98, 234, 251, 237, 24, 143, 210, 30, 242, 97, 66, 50, 211, 47, 109, 110, 121, 244, 239, 89, 0, 39, 92, 218, 155, 71, 5, 23, 136, 231, 107, 95, 52, 231, 118, 253, 206, 250, 178, 209, 136, 13, 36, 36, 54, 157, 237, 35, 110, 134, 253, 80, 237, 162, 163, 38, 21, 54, 241, 240, 253, 73, 33, 191, 128, 32, 6, 198, 165, 35, 203, 244, 15, 166, 250, 159, 67, 149, 56, 19, 243, 230, 87, 6, 44, 150, 90, 79, 107, 18, 121, 112, 23, 176, 104, 50, 110, 176, 138, 250, 6, 209, 22, 41, 73, 234, 4, 124, 233, 208, 218, 236, 117, 232, 217, 10, 172, 18, 215, 143, 119, 193, 113, 10, 59, 255, 221, 0, 0, 41, 16, 0, 0, 0, 128, 0, 0, 0}}, 1411 }, 1412 ci: gopacket.CaptureInfo{ 1413 Timestamp: time.Unix(1432538521, 622508000), 1414 }, 1415 nb: 8, 1416 }, 1417 { 1418 tcp: layers.TCP{ 1419 ACK: true, 1420 SrcPort: 54842, 1421 DstPort: 53, 1422 Seq: 374511150, 1423 Ack: 3465789949, 1424 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1425 }, 1426 ci: gopacket.CaptureInfo{ 1427 Timestamp: time.Unix(1432538521, 622531000), 1428 }, 1429 nb: 9, 1430 }, 1431 { 1432 tcp: layers.TCP{ 1433 ACK: true, 1434 FIN: true, 1435 SrcPort: 54842, 1436 DstPort: 53, 1437 Seq: 374511150, 1438 Ack: 3465789949, 1439 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1440 }, 1441 ci: gopacket.CaptureInfo{ 1442 Timestamp: time.Unix(1432538521, 622907000), 1443 }, 1444 nb: 10, 1445 }, 1446 { 1447 tcp: layers.TCP{ 1448 ACK: true, 1449 FIN: true, 1450 SrcPort: 53, 1451 DstPort: 54842, 1452 Seq: 3465789949, 1453 Ack: 374511151, 1454 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1455 }, 1456 ci: gopacket.CaptureInfo{ 1457 Timestamp: time.Unix(1432538521, 652784000), 1458 }, 1459 nb: 11, 1460 }, 1461 { 1462 tcp: layers.TCP{ 1463 ACK: true, 1464 SrcPort: 54842, 1465 DstPort: 53, 1466 Seq: 374511151, 1467 Ack: 3465789950, 1468 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1469 }, 1470 ci: gopacket.CaptureInfo{ 1471 Timestamp: time.Unix(1432538521, 652809000), 1472 }, 1473 nb: 12, 1474 }, 1475 }) 1476} 1477 1478func TestFSMearlyRST(t *testing.T) { 1479 testFSM(t, []testFSMSequence{ 1480 { 1481 tcp: layers.TCP{ 1482 SYN: true, 1483 SrcPort: 54842, 1484 DstPort: 53, 1485 Seq: 374511116, 1486 Ack: 0, 1487 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1488 }, 1489 ci: gopacket.CaptureInfo{ 1490 Timestamp: time.Unix(1432538521, 566690000), 1491 }, 1492 nb: 1, 1493 }, 1494 { 1495 tcp: layers.TCP{ 1496 SYN: true, 1497 ACK: true, 1498 SrcPort: 53, 1499 DstPort: 54842, 1500 Seq: 3465787765, 1501 Ack: 374511117, 1502 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1503 }, 1504 ci: gopacket.CaptureInfo{ 1505 Timestamp: time.Unix(1432538521, 590332000), 1506 }, 1507 nb: 2, 1508 }, 1509 { 1510 tcp: layers.TCP{ 1511 RST: true, 1512 SrcPort: 54842, 1513 DstPort: 53, 1514 Seq: 374511117, 1515 Ack: 3465787766, 1516 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1517 }, 1518 ci: gopacket.CaptureInfo{ 1519 Timestamp: time.Unix(1432538521, 590346000), 1520 }, 1521 nb: 3, 1522 }, 1523 { 1524 tcp: layers.TCP{ 1525 ACK: true, 1526 SrcPort: 54842, 1527 DstPort: 53, 1528 Seq: 374511117, 1529 Ack: 3465787766, 1530 BaseLayer: layers.BaseLayer{Payload: []byte{0, 31, 104, 196, 0, 32, 0, 1, 0, 0, 0, 0, 0, 1, 2, 85, 83, 0, 0, 6, 0, 1, 0, 0, 41, 16, 0, 0, 0, 128, 0, 0, 0}}, 1531 }, 1532 ci: gopacket.CaptureInfo{ 1533 Timestamp: time.Unix(1432538521, 590387000), 1534 }, 1535 nb: 3, 1536 }, 1537 { 1538 tcp: layers.TCP{ 1539 ACK: true, 1540 SrcPort: 53, 1541 DstPort: 54842, 1542 Seq: 3465787766, 1543 Ack: 374511150, 1544 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1545 }, 1546 ci: gopacket.CaptureInfo{ 1547 Timestamp: time.Unix(1432538521, 613687000), 1548 }, 1549 nb: 3, 1550 }, 1551 }) 1552} 1553 1554func TestFSMestablishedThenRST(t *testing.T) { 1555 testFSM(t, []testFSMSequence{ 1556 { 1557 tcp: layers.TCP{ 1558 SYN: true, 1559 SrcPort: 54842, 1560 DstPort: 53, 1561 Seq: 374511116, 1562 Ack: 0, 1563 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1564 }, 1565 ci: gopacket.CaptureInfo{ 1566 Timestamp: time.Unix(1432538521, 566690000), 1567 }, 1568 nb: 1, 1569 }, 1570 { 1571 tcp: layers.TCP{ 1572 SYN: true, 1573 ACK: true, 1574 SrcPort: 53, 1575 DstPort: 54842, 1576 Seq: 3465787765, 1577 Ack: 374511117, 1578 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1579 }, 1580 ci: gopacket.CaptureInfo{ 1581 Timestamp: time.Unix(1432538521, 590332000), 1582 }, 1583 nb: 2, 1584 }, 1585 { 1586 tcp: layers.TCP{ 1587 ACK: true, 1588 SrcPort: 54842, 1589 DstPort: 53, 1590 Seq: 374511117, 1591 Ack: 3465787766, 1592 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1593 }, 1594 ci: gopacket.CaptureInfo{ 1595 Timestamp: time.Unix(1432538521, 590346000), 1596 }, 1597 nb: 3, 1598 }, 1599 { 1600 tcp: layers.TCP{ 1601 ACK: true, 1602 SrcPort: 54842, 1603 DstPort: 53, 1604 Seq: 374511117, 1605 Ack: 3465787766, 1606 BaseLayer: layers.BaseLayer{Payload: []byte{0, 31, 104, 196, 0, 32, 0, 1, 0, 0, 0, 0, 0, 1, 2, 85, 83, 0, 0, 6, 0, 1, 0, 0, 41, 16, 0, 0, 0, 128, 0, 0, 0}}, 1607 }, 1608 ci: gopacket.CaptureInfo{ 1609 Timestamp: time.Unix(1432538521, 590387000), 1610 }, 1611 nb: 4, 1612 }, 1613 { 1614 tcp: layers.TCP{ 1615 RST: true, 1616 SrcPort: 53, 1617 DstPort: 54842, 1618 Seq: 3465787766, 1619 Ack: 374511150, 1620 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1621 }, 1622 ci: gopacket.CaptureInfo{ 1623 Timestamp: time.Unix(1432538521, 613687000), 1624 }, 1625 nb: 5, 1626 }, 1627 { 1628 tcp: layers.TCP{ 1629 ACK: true, 1630 SrcPort: 53, 1631 DstPort: 54842, 1632 Seq: 3465787766, 1633 Ack: 374511150, 1634 BaseLayer: layers.BaseLayer{Payload: []byte{8, 133, 104, 196, 132, 0, 0, 1, 0, 2, 0, 7, 0, 19, 2, 85, 83, 0, 0, 6, 0, 1, 2, 117, 115, 0, 0, 6, 0, 1, 0, 0, 3, 132, 0, 54, 1, 97, 5, 99, 99, 116, 108, 100, 192, 20, 10, 104, 111, 115, 116, 109, 97, 115, 116, 101, 114, 7, 110, 101, 117, 115, 116, 97, 114, 3, 98, 105, 122, 0, 120, 18, 40, 205, 0, 0, 3, 132, 0, 0, 3, 132, 0, 9, 58, 128, 0, 1, 81, 128, 192, 20, 0, 46, 0, 1, 0, 0, 3, 132, 0, 150, 0, 6, 5, 1, 0, 0, 3, 132, 85, 138, 90, 146, 85, 98, 191, 130, 27, 78, 2, 117, 115, 0, 69, 13, 35, 189, 141, 225, 107, 238, 108, 182, 207, 44, 105, 31, 212, 103, 32, 93, 217, 108, 20, 231, 188, 28, 241, 237, 104, 182, 117, 121, 195, 112, 64, 96, 237, 248, 6, 181, 186, 96, 60, 6, 18, 29, 188, 96, 201, 140, 251, 61, 71, 177, 108, 156, 9, 83, 125, 172, 188, 75, 81, 67, 218, 55, 93, 131, 243, 15, 190, 75, 4, 165, 226, 124, 49, 67, 142, 131, 239, 240, 76, 225, 10, 242, 68, 88, 240, 200, 27, 97, 102, 73, 92, 73, 133, 170, 175, 198, 99, 109, 90, 16, 162, 101, 95, 96, 102, 250, 91, 74, 80, 3, 87, 167, 50, 230, 9, 213, 7, 222, 197, 87, 183, 190, 148, 247, 207, 204, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 10, 1, 102, 5, 99, 99, 116, 108, 100, 192, 12, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 97, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 98, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 99, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 101, 193, 8, 192, 118, 0, 2, 0, 1, 0, 7, 233, 0, 0, 4, 1, 107, 193, 8, 192, 118, 0, 46, 0, 1, 0, 7, 233, 0, 0, 150, 0, 2, 5, 1, 0, 7, 233, 0, 85, 127, 33, 92, 85, 87, 134, 98, 27, 78, 2, 117, 115, 0, 19, 227, 175, 75, 88, 245, 164, 158, 150, 198, 57, 253, 150, 179, 161, 52, 24, 56, 229, 176, 175, 40, 45, 232, 188, 171, 131, 197, 107, 125, 218, 192, 78, 221, 146, 33, 114, 55, 43, 12, 131, 213, 51, 98, 37, 2, 102, 161, 232, 115, 177, 210, 51, 169, 215, 133, 56, 190, 91, 75, 8, 222, 231, 202, 139, 28, 187, 249, 72, 21, 23, 56, 63, 72, 126, 142, 242, 195, 242, 64, 208, 134, 100, 157, 197, 159, 43, 148, 20, 70, 117, 152, 159, 35, 200, 220, 49, 234, 173, 210, 91, 34, 210, 192, 7, 197, 112, 117, 208, 234, 42, 49, 133, 237, 197, 14, 244, 149, 191, 142, 36, 252, 42, 48, 182, 189, 9, 68, 1, 65, 5, 67, 67, 84, 76, 68, 193, 126, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 124, 70, 1, 66, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 125, 70, 194, 26, 0, 28, 0, 1, 0, 0, 28, 32, 0, 16, 32, 1, 5, 3, 209, 174, 255, 255, 255, 255, 255, 255, 255, 255, 255, 126, 1, 67, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 127, 70, 1, 69, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 126, 70, 1, 70, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 209, 173, 58, 70, 194, 108, 0, 28, 0, 1, 0, 0, 28, 32, 0, 16, 32, 1, 5, 0, 54, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 1, 75, 194, 4, 0, 1, 0, 1, 0, 0, 28, 32, 0, 4, 156, 154, 128, 70, 194, 154, 0, 28, 0, 1, 0, 0, 28, 32, 0, 16, 32, 1, 5, 3, 226, 57, 0, 0, 0, 0, 0, 0, 0, 3, 0, 1, 194, 2, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 112, 230, 49, 85, 73, 83, 2, 27, 78, 2, 117, 115, 0, 82, 36, 11, 141, 74, 85, 70, 98, 179, 63, 173, 83, 8, 70, 155, 41, 102, 166, 140, 62, 71, 178, 130, 38, 171, 200, 180, 68, 2, 215, 45, 6, 43, 59, 171, 146, 223, 215, 9, 77, 5, 104, 167, 42, 237, 170, 30, 114, 205, 129, 59, 225, 152, 224, 79, 1, 65, 68, 208, 153, 121, 237, 199, 87, 2, 251, 100, 105, 59, 24, 73, 226, 169, 121, 250, 91, 41, 124, 14, 23, 135, 52, 2, 86, 72, 224, 100, 135, 70, 216, 16, 107, 84, 59, 13, 168, 58, 187, 54, 98, 230, 167, 246, 42, 46, 156, 206, 238, 120, 199, 25, 144, 98, 249, 70, 162, 34, 43, 145, 114, 186, 233, 47, 42, 75, 95, 152, 235, 194, 26, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 112, 190, 140, 85, 73, 36, 78, 27, 78, 2, 117, 115, 0, 160, 95, 100, 37, 167, 82, 93, 165, 126, 247, 147, 173, 238, 154, 206, 174, 96, 175, 209, 7, 8, 169, 171, 223, 29, 201, 161, 177, 98, 54, 94, 62, 70, 127, 142, 109, 206, 42, 179, 109, 156, 160, 156, 20, 59, 24, 147, 164, 13, 121, 192, 84, 157, 26, 56, 177, 151, 210, 7, 197, 229, 110, 60, 58, 224, 42, 77, 5, 59, 80, 216, 221, 248, 19, 66, 102, 74, 199, 238, 120, 231, 201, 187, 29, 11, 46, 195, 164, 8, 221, 128, 25, 205, 42, 247, 152, 112, 176, 14, 117, 150, 223, 245, 32, 212, 107, 4, 245, 27, 126, 224, 216, 0, 89, 106, 238, 185, 206, 44, 56, 204, 175, 7, 139, 233, 228, 127, 175, 194, 26, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 28, 5, 3, 0, 0, 28, 32, 85, 108, 217, 174, 85, 69, 70, 242, 27, 78, 2, 117, 115, 0, 172, 117, 89, 89, 73, 249, 245, 211, 100, 127, 48, 135, 224, 97, 172, 146, 128, 30, 190, 72, 199, 170, 97, 179, 136, 109, 86, 110, 235, 214, 47, 50, 115, 11, 226, 168, 56, 198, 24, 212, 205, 207, 2, 116, 104, 112, 99, 234, 236, 44, 70, 19, 19, 215, 127, 200, 162, 215, 142, 45, 135, 91, 219, 217, 86, 231, 154, 87, 222, 161, 32, 66, 196, 55, 117, 20, 186, 9, 134, 252, 249, 219, 9, 196, 128, 8, 222, 201, 131, 210, 182, 232, 142, 72, 160, 171, 95, 231, 232, 156, 28, 34, 54, 94, 73, 183, 38, 160, 123, 175, 157, 21, 163, 8, 214, 155, 172, 237, 169, 28, 15, 138, 105, 107, 251, 109, 131, 240, 194, 72, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 112, 190, 140, 85, 73, 36, 78, 27, 78, 2, 117, 115, 0, 77, 207, 197, 130, 236, 138, 192, 241, 225, 114, 8, 22, 76, 54, 43, 121, 42, 44, 9, 92, 56, 253, 224, 179, 191, 131, 40, 176, 94, 61, 33, 12, 43, 82, 156, 236, 211, 29, 187, 100, 220, 243, 24, 134, 42, 204, 46, 161, 214, 91, 68, 119, 40, 252, 53, 54, 146, 136, 196, 168, 204, 195, 131, 110, 6, 73, 16, 161, 86, 35, 150, 153, 162, 185, 227, 65, 228, 160, 203, 42, 250, 121, 14, 42, 115, 221, 232, 96, 99, 164, 230, 29, 195, 149, 85, 206, 41, 1, 252, 77, 188, 88, 8, 182, 37, 249, 6, 158, 6, 244, 158, 254, 141, 203, 6, 158, 198, 103, 130, 98, 123, 34, 245, 44, 126, 77, 24, 187, 194, 90, 0, 46, 0, 1, 0, 0, 28, 32, 0, 150, 0, 1, 5, 3, 0, 0, 28, 32, 85, 108, 194, 203, 85, 69, 51, 125, 27, 78, 2, 117, 115, 0, 86, 26, 187, 56, 252, 194, 199, 140, 229, 133, 186, 187, 20, 174, 26, 48, 212, 129, 10, 20, 167, 179, 53, 72, 176, 92, 153, 48, 146, 15, 163, 182, 80, 138, 181, 135, 98, 129, 17, 66, 55, 184, 76, 225, 72, 104, 7, 221, 40, 71, 41, 202, 246, 154, 166, 199, 74, 175, 146, 54, 25, 56, 115, 243}}, 1635 }, 1636 ci: gopacket.CaptureInfo{ 1637 Timestamp: time.Unix(1432538521, 621198000), 1638 }, 1639 nb: 5, 1640 }, 1641 { 1642 tcp: layers.TCP{ 1643 ACK: true, 1644 SrcPort: 54842, 1645 DstPort: 53, 1646 Seq: 374511150, 1647 Ack: 3465789226, 1648 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1649 }, 1650 ci: gopacket.CaptureInfo{ 1651 Timestamp: time.Unix(1432538521, 621220000), 1652 }, 1653 nb: 5, 1654 }, 1655 }) 1656} 1657 1658func TestFSMmissingSYNACK(t *testing.T) { 1659 testFSM(t, []testFSMSequence{ 1660 { 1661 tcp: layers.TCP{ 1662 SYN: true, 1663 SrcPort: 54842, 1664 DstPort: 53, 1665 Seq: 374511116, 1666 Ack: 0, 1667 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1668 }, 1669 ci: gopacket.CaptureInfo{ 1670 Timestamp: time.Unix(1432538521, 566690000), 1671 }, 1672 nb: 1, 1673 }, 1674 { 1675 tcp: layers.TCP{ 1676 ACK: true, 1677 SrcPort: 54842, 1678 DstPort: 53, 1679 Seq: 374511117, 1680 Ack: 3465787766, 1681 BaseLayer: layers.BaseLayer{Payload: []byte{}}, 1682 }, 1683 ci: gopacket.CaptureInfo{ 1684 Timestamp: time.Unix(1432538521, 590346000), 1685 }, 1686 nb: 1, 1687 }, 1688 { 1689 tcp: layers.TCP{ 1690 ACK: true, 1691 SrcPort: 54842, 1692 DstPort: 53, 1693 Seq: 374511117, 1694 Ack: 3465787766, 1695 BaseLayer: layers.BaseLayer{Payload: []byte{0, 31, 104, 196, 0, 32, 0, 1, 0, 0, 0, 0, 0, 1, 2, 85, 83, 0, 0, 6, 0, 1, 0, 0, 41, 16, 0, 0, 0, 128, 0, 0, 0}}, 1696 }, 1697 ci: gopacket.CaptureInfo{ 1698 Timestamp: time.Unix(1432538521, 590387000), 1699 }, 1700 nb: 1, 1701 }, 1702 }) 1703} 1704 1705/* 1706 * Memory test 1707 */ 1708func TestMemoryShrink(t *testing.T) { 1709 tcp := layers.TCP{ 1710 SrcPort: 1, 1711 DstPort: 2, 1712 SYN: true, 1713 Seq: 999, 1714 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1715 } 1716 a := NewAssembler(NewStreamPool(&testFactoryBench{})) 1717 var before runtime.MemStats 1718 runtime.GC() 1719 runtime.ReadMemStats(&before) 1720 run := 1050 1721 // Allocate > initial 1722 for i := 0; i < run; i++ { 1723 a.Assemble(netFlow, &tcp) 1724 if tcp.SYN { 1725 tcp.SYN = false 1726 tcp.Seq += 1 + 1 1727 } 1728 tcp.Seq += 10 1729 } 1730 var after runtime.MemStats 1731 a.FlushAll() 1732 runtime.GC() 1733 runtime.ReadMemStats(&after) 1734 if after.HeapAlloc < before.HeapAlloc { 1735 t.Fatalf("Nothing allocated for %d run: before: %d, after: %d", run, before.HeapAlloc, after.HeapAlloc) 1736 } 1737 before = after 1738 // Do ~ initial allocs+free() 1739 run *= 2 1740 for i := 0; i < run; i++ { 1741 a.Assemble(netFlow, &tcp) 1742 if i%50 == 0 { 1743 a.FlushAll() 1744 } 1745 tcp.Seq += 10 1746 } 1747 runtime.GC() 1748 runtime.ReadMemStats(&after) 1749 if after.HeapAlloc >= before.HeapAlloc { 1750 t.Fatalf("Nothing freed for %d run: before: %d, after: %d", run, before.HeapAlloc, after.HeapAlloc) 1751 } 1752} 1753 1754/* 1755 * Benchmark tests 1756 */ 1757func BenchmarkSingleStreamNo(b *testing.B) { 1758 t := layers.TCP{ 1759 SrcPort: 1, 1760 DstPort: 2, 1761 SYN: true, 1762 Seq: 1000, 1763 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1764 } 1765 a := NewAssembler(NewStreamPool(&testFactoryBench{})) 1766 for i := 0; i < b.N; i++ { 1767 a.Assemble(netFlow, &t) 1768 if t.SYN { 1769 t.SYN = false 1770 t.Seq++ 1771 } 1772 t.Seq += 10 1773 } 1774} 1775 1776func BenchmarkSingleStreamSkips(b *testing.B) { 1777 t := layers.TCP{ 1778 SrcPort: 1, 1779 DstPort: 2, 1780 SYN: true, 1781 Seq: 1000, 1782 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1783 } 1784 a := NewAssembler(NewStreamPool(&testFactoryBench{})) 1785 skipped := false 1786 for i := 0; i < b.N; i++ { 1787 if i%10 == 9 { 1788 t.Seq += 10 1789 skipped = true 1790 } else if skipped { 1791 t.Seq -= 20 1792 } 1793 a.Assemble(netFlow, &t) 1794 if t.SYN { 1795 t.SYN = false 1796 t.Seq++ 1797 } 1798 t.Seq += 10 1799 if skipped { 1800 t.Seq += 10 1801 skipped = false 1802 } 1803 } 1804} 1805 1806func BenchmarkSingleStreamLoss(b *testing.B) { 1807 t := layers.TCP{ 1808 SrcPort: 1, 1809 DstPort: 2, 1810 SYN: true, 1811 Seq: 1000, 1812 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1813 } 1814 a := NewAssembler(NewStreamPool(&testFactoryBench{})) 1815 for i := 0; i < b.N; i++ { 1816 a.Assemble(netFlow, &t) 1817 t.SYN = false 1818 t.Seq += 11 1819 } 1820} 1821 1822func BenchmarkMultiStreamGrow(b *testing.B) { 1823 t := layers.TCP{ 1824 SrcPort: 1, 1825 DstPort: 2, 1826 Seq: 0, 1827 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1828 } 1829 a := NewAssembler(NewStreamPool(&testFactoryBench{})) 1830 for i := 0; i < b.N; i++ { 1831 t.SrcPort = layers.TCPPort(i) 1832 a.Assemble(netFlow, &t) 1833 t.Seq += 10 1834 } 1835} 1836 1837func BenchmarkMultiStreamConn(b *testing.B) { 1838 t := layers.TCP{ 1839 SrcPort: 1, 1840 DstPort: 2, 1841 Seq: 0, 1842 SYN: true, 1843 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1844 } 1845 a := NewAssembler(NewStreamPool(&testFactoryBench{})) 1846 for i := 0; i < b.N; i++ { 1847 t.SrcPort = layers.TCPPort(i) 1848 a.Assemble(netFlow, &t) 1849 if i%65536 == 65535 { 1850 if t.SYN { 1851 t.SYN = false 1852 t.Seq++ 1853 } 1854 t.Seq += 10 1855 } 1856 } 1857} 1858 1859type testMemoryContext struct{} 1860 1861func (t *testMemoryContext) GetCaptureInfo() gopacket.CaptureInfo { 1862 return gopacket.CaptureInfo{ 1863 Timestamp: time.Unix(1432538521, 590387000), 1864 } 1865} 1866 1867func TestFullyOrderedAndCompleteStreamDoesNotAlloc(t *testing.T) { 1868 c2s := layers.TCP{ 1869 SrcPort: 1, 1870 DstPort: 2, 1871 Seq: 0, 1872 SYN: true, 1873 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1874 } 1875 s2c := layers.TCP{ 1876 SrcPort: c2s.DstPort, 1877 DstPort: c2s.SrcPort, 1878 Seq: 0, 1879 SYN: true, 1880 ACK: true, 1881 BaseLayer: layers.BaseLayer{Payload: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}}, 1882 } 1883 tf := testMemoryFactory{} 1884 a := NewAssembler(NewStreamPool(&tf)) 1885 1886 ctx := &testMemoryContext{} 1887 // First packet 1888 a.AssembleWithContext(netFlow, &c2s, ctx) 1889 a.AssembleWithContext(netFlow.Reverse(), &s2c, ctx) 1890 c2s.SYN, s2c.SYN = false, false 1891 c2s.ACK = true 1892 c2s.Seq++ 1893 s2c.Seq++ 1894 N := 1000 1895 if n := testing.AllocsPerRun(N, func() { 1896 c2s.Seq += 10 1897 s2c.Seq += 10 1898 c2s.Ack += 10 1899 s2c.Ack += 10 1900 a.AssembleWithContext(netFlow, &c2s, ctx) 1901 a.AssembleWithContext(netFlow.Reverse(), &s2c, ctx) 1902 }); n > 0 { 1903 t.Error(n, "mallocs for normal TCP stream") 1904 } 1905 // Ensure all bytes have been through the stream 1906 // +1 for first packet and +1 because AllocsPerRun seems to run fun N+1 times. 1907 if tf.bytes != 10*2*(N+1+1) { 1908 t.Error(tf.bytes, "bytes handled, expected", 10*2*(N+1+1)) 1909 } 1910} 1911 1912type testCustomContext int 1913 1914func (c testCustomContext) GetCaptureInfo() gopacket.CaptureInfo { 1915 // We're just abusing the InterfaceIndex to identify the context, no other 1916 // meaning here. 1917 return gopacket.CaptureInfo{InterfaceIndex: int(c)} 1918} 1919 1920// Make sure reassemblyObject.CaptureInfo conforms to ScatterGather interface. 1921func TestReassemblyObjectCaptureInfo(t *testing.T) { 1922 // Add 20 bytes worth of data into a reassemblyObject. 1923 all := []byteContainer{ 1924 &page{ 1925 bytes: bytes.Repeat([]byte("1"), 10), 1926 ac: testCustomContext(1203), 1927 }, 1928 &livePacket{ 1929 bytes: bytes.Repeat([]byte("1"), 10), 1930 ac: testCustomContext(794598214), 1931 }, 1932 } 1933 ro := &reassemblyObject{all: all} 1934 1935 testCases := []struct { 1936 offset int 1937 expected testCustomContext 1938 }{ 1939 { 1940 offset: -1, 1941 }, 1942 { 1943 offset: 0, 1944 expected: testCustomContext(1203), 1945 }, 1946 { 1947 offset: 5, 1948 expected: testCustomContext(1203), 1949 }, 1950 { 1951 offset: 10, 1952 expected: testCustomContext(794598214), 1953 }, 1954 { 1955 offset: 19, 1956 expected: testCustomContext(794598214), 1957 }, 1958 { 1959 offset: 20, 1960 }, 1961 { 1962 offset: 1000000, 1963 }, 1964 } 1965 for _, c := range testCases { 1966 expected := c.expected.GetCaptureInfo() 1967 ci := ro.CaptureInfo(c.offset) 1968 if !reflect.DeepEqual(expected, ci) { 1969 t.Errorf("test CaptureInfo(%d):\nwant: %v\n got: %v\n", c.offset, expected, ci) 1970 } 1971 } 1972} 1973