rewrite_sequence.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "rewrite_sequence.h"
  25. #include "config.h"
  26. #include "incremental_checksum.h"
  27. #include "tcpedit.h"
  28. #include <stdlib.h>
  29. /**
  30. * rewrites the TCP sequence and ack numbers
  31. * returns 1 for changes made or 0 for none
  32. */
  33. static int
  34. rewrite_seqs(tcpedit_t *tcpedit, tcp_hdr_t *tcp_hdr)
  35. {
  36. volatile uint32_t newnum;
  37. newnum = ntohl(tcp_hdr->th_seq) + tcpedit->tcp_sequence_adjust;
  38. csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_seq, htonl(newnum));
  39. tcp_hdr->th_seq = htonl(newnum);
  40. /* first packet of 3-way handshake must have an ACK of zero - #450 */
  41. if (!((tcp_hdr->th_flags & TH_SYN) && !(tcp_hdr->th_flags & TH_ACK))) {
  42. newnum = ntohl(tcp_hdr->th_ack) + tcpedit->tcp_sequence_adjust;
  43. csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_ack, htonl(newnum));
  44. tcp_hdr->th_ack = htonl(newnum);
  45. }
  46. return 0;
  47. }
  48. int
  49. rewrite_ipv4_tcp_sequence(tcpedit_t *tcpedit, ipv4_hdr_t **ip_hdr, int l3len)
  50. {
  51. assert(tcpedit);
  52. assert(*ip_hdr && ip_hdr);
  53. if (*ip_hdr && (*ip_hdr)->ip_p == IPPROTO_TCP) {
  54. tcp_hdr_t *tcp_hdr = (tcp_hdr_t *)get_layer4_v4(*ip_hdr, (u_char *)ip_hdr + l3len);
  55. if (!tcp_hdr) {
  56. tcpedit_setwarn(tcpedit, "caplen to small to set TCP sequence for IP packet: l3 len=%d", l3len);
  57. return TCPEDIT_WARN;
  58. }
  59. return rewrite_seqs(tcpedit, tcp_hdr);
  60. }
  61. return 0;
  62. }
  63. int
  64. rewrite_ipv6_tcp_sequence(tcpedit_t *tcpedit, ipv6_hdr_t **ip6_hdr, int l3len)
  65. {
  66. assert(tcpedit);
  67. assert(*ip6_hdr && ip6_hdr);
  68. if (*ip6_hdr && (*ip6_hdr)->ip_nh == IPPROTO_TCP) {
  69. tcp_hdr_t *tcp_hdr = (tcp_hdr_t *)get_layer4_v6(*ip6_hdr, (u_char *)ip6_hdr + l3len);
  70. if (!tcp_hdr) {
  71. tcpedit_setwarn(tcpedit, "caplen to small to set TCP sequence for IP packet: l3 len=%d", l3len);
  72. return TCPEDIT_WARN;
  73. }
  74. return rewrite_seqs(tcpedit, tcp_hdr);
  75. }
  76. return 0;
  77. }