rewrite_sequence.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. * Copyright (c) 2017 Mario D. Santana <tcpreplay at elorangutan dot com> - El Orangutan
  6. *
  7. * The Tcpreplay Suite of tools is free software: you can redistribute it
  8. * and/or modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or with the authors permission any later version.
  11. *
  12. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. * This file contains routines to manipulate port maps, in which
  22. * one port number is mapped to another.
  23. */
  24. #include "config.h"
  25. #include "defines.h"
  26. #include "common.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include "tcpreplay.h"
  31. #include "tcpedit.h"
  32. #include "rewrite_sequence.h"
  33. #include "incremental_checksum.h"
  34. /**
  35. * rewrites the TCP sequence and ack numbers
  36. * returns 1 for changes made or 0 for none
  37. */
  38. static int
  39. rewrite_seqs(tcpedit_t *tcpedit, tcp_hdr_t *tcp_hdr)
  40. {
  41. volatile uint32_t newnum;
  42. newnum = ntohl(tcp_hdr->th_seq) + tcpedit->tcp_sequence_adjust;
  43. csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_seq, htonl(newnum));
  44. tcp_hdr->th_seq = htonl(newnum);
  45. /* first packet of 3-way handshake must have an ACK of zero - #450 */
  46. if (!((tcp_hdr->th_flags & TH_SYN) && !(tcp_hdr->th_flags & TH_ACK))) {
  47. newnum = ntohl(tcp_hdr->th_ack) + tcpedit->tcp_sequence_adjust;
  48. csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_ack, htonl(newnum));
  49. tcp_hdr->th_ack = htonl(newnum);
  50. }
  51. return 0;
  52. }
  53. int
  54. rewrite_ipv4_tcp_sequence(tcpedit_t *tcpedit, ipv4_hdr_t **ip_hdr,
  55. const int l3len)
  56. {
  57. assert(tcpedit);
  58. assert(*ip_hdr && ip_hdr);
  59. if (*ip_hdr && (*ip_hdr)->ip_p == IPPROTO_TCP) {
  60. tcp_hdr_t *tcp_hdr = (tcp_hdr_t *)get_layer4_v4(*ip_hdr, l3len);
  61. if (!tcp_hdr) {
  62. tcpedit_setwarn(tcpedit, "caplen to small to set TCP sequence for IP packet: l3 len=%d",
  63. l3len);
  64. return TCPEDIT_WARN;
  65. }
  66. return rewrite_seqs(tcpedit, tcp_hdr);
  67. }
  68. return 0;
  69. }
  70. int
  71. rewrite_ipv6_tcp_sequence(tcpedit_t *tcpedit, ipv6_hdr_t **ip6_hdr,
  72. const int l3len)
  73. {
  74. assert(tcpedit);
  75. assert(*ip6_hdr && ip6_hdr);
  76. if (*ip6_hdr && (*ip6_hdr)->ip_nh == IPPROTO_TCP) {
  77. tcp_hdr_t *tcp_hdr = (tcp_hdr_t *)get_layer4_v6(*ip6_hdr, l3len);
  78. if (!tcp_hdr) {
  79. tcpedit_setwarn(tcpedit, "caplen to small to set TCP sequence for IP packet: l3 len=%d",
  80. l3len);
  81. return TCPEDIT_WARN;
  82. }
  83. return rewrite_seqs(tcpedit, tcp_hdr);
  84. }
  85. return 0;
  86. }