flowstate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* $Id: flowstate.c 1477 2006-07-08 03:54:51Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2004 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include "flowreplay.h"
  35. #include "flownode.h"
  36. #include "flowstate.h"
  37. /*
  38. * determines the new state for a TCP flow based on
  39. * the last known state and the current packet
  40. * returns the new state as well as setting it in the node
  41. */
  42. u_int32_t
  43. tcp_state(tcp_hdr_t * tcp_hdr, struct session_t *node)
  44. {
  45. /*
  46. * figure out the TCP state
  47. */
  48. if (node->state == 0x0) {
  49. /*
  50. * We go here if this is the first packet in the
  51. * in the TCP stream. This could be a Syn or
  52. * if we're trying to pickup the state from mid-stream
  53. */
  54. /* = Syn, start of new flow */
  55. if (tcp_hdr->th_flags & TH_SYN) {
  56. node->state = TH_SYN;
  57. dbg(3, "Setting state: New -> Syn");
  58. }
  59. /* Anything matching after this point is a mid-stream pickup */
  60. /* + Ack */
  61. if (tcp_hdr->th_flags & TH_ACK) {
  62. node->state ^= TH_ACK;
  63. dbg(3, "Mid-stream state pickup: +Ack");
  64. }
  65. /* = Fin */
  66. if (tcp_hdr->th_flags & TH_FIN) {
  67. node->state = TH_FIN;
  68. dbg(3, "Mid-stream state pickup: Fin");
  69. }
  70. /* else, just close */
  71. if (!node->state) {
  72. node->state = TCP_CLOSE;
  73. dbg(3, "Mid-stream state pickup: Close");
  74. }
  75. }
  76. /* look for a Syn/Ack while we're in Syn */
  77. else if ((tcp_hdr->th_flags & TH_SYN) &&
  78. (tcp_hdr->th_flags & TH_ACK) && (node->state == TH_SYN)) {
  79. /* server sent SYN/ACK */
  80. node->state = TH_SYN | TH_ACK;
  81. dbg(4, "Setting state: Syn -> Syn/Ack");
  82. }
  83. else if ((tcp_hdr->th_flags & TH_ACK) &&
  84. (node->state & TH_SYN) && (node->state & TH_ACK)) {
  85. /* Client sent ACK when we're Syn/Ack */
  86. node->state = TH_ACK;
  87. dbg(4, "Setting state: Syn/Ack -> Ack");
  88. }
  89. /* someone sent us the FIN */
  90. else if (tcp_hdr->th_flags & TH_FIN) {
  91. if (node->state == TH_ACK) {
  92. /* first FIN */
  93. node->state = TH_FIN;
  94. dbg(4, "Setting state: Ack -> Fin");
  95. }
  96. else {
  97. /* second FIN, close connection */
  98. dbg(4, "Setting state: Fin -> Close");
  99. node->state = TCP_CLOSE;
  100. }
  101. }
  102. /* Reset */
  103. else if (tcp_hdr->th_flags & TH_RST) {
  104. dbg(4, "Reset packet! Setting state: Rst");
  105. node->state = TCP_CLOSE;
  106. }
  107. else if ((node->state == TH_ACK) && (tcp_hdr->th_flags & TH_ACK)) {
  108. dbg(3, "No state change: Ack");
  109. }
  110. else {
  111. warnx("Unable to determine TCP state for node 0x%llx",
  112. pkeygen(node->key));
  113. }
  114. return node->state;
  115. }
  116. /*
  117. Local Variables:
  118. mode:c
  119. indent-tabs-mode:nil
  120. c-basic-offset:4
  121. End:
  122. */