checksum.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2022 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * This code is heavily based on (some might even say stolen from) Mike Shiffman's
  21. * checksumming code from Libnet 1.1.3
  22. */
  23. #include "config.h"
  24. #include "tcpedit.h"
  25. #include "checksum.h"
  26. static int do_checksum_math(uint16_t *, int);
  27. /**
  28. * Returns -1 on error and 0 on success, 1 on warn
  29. */
  30. int
  31. do_checksum(tcpedit_t *tcpedit,
  32. uint8_t *data,
  33. int proto,
  34. int len,
  35. const u_char *end_ptr)
  36. {
  37. ipv4_hdr_t *ipv4;
  38. ipv6_hdr_t *ipv6;
  39. tcp_hdr_t *tcp;
  40. udp_hdr_t *udp;
  41. icmpv4_hdr_t *icmp;
  42. icmpv6_hdr_t *icmp6;
  43. u_char *layer;
  44. int ip_hl;
  45. int sum;
  46. sum = 0;
  47. ipv4 = NULL;
  48. ipv6 = NULL;
  49. assert(data);
  50. if (!data || len <= 0) {
  51. tcpedit_seterr(tcpedit, "%s", "length of data must be > 0");
  52. return TCPEDIT_ERROR;
  53. }
  54. ipv4 = (ipv4_hdr_t *)data;
  55. if (ipv4->ip_v == 6) {
  56. ipv6 = (ipv6_hdr_t *)data;
  57. ipv4 = NULL;
  58. proto = get_ipv6_l4proto(ipv6, end_ptr);
  59. dbgx(3, "layer4 proto is 0x%hx", (uint16_t)proto);
  60. layer = (u_char*)get_layer4_v6(ipv6, end_ptr);
  61. if (!layer) {
  62. tcpedit_setwarn(tcpedit, "%s", "Packet to short for checksum");
  63. return TCPEDIT_WARN;
  64. }
  65. ip_hl = layer - (u_char*)data;
  66. dbgx(3, "ip_hl proto is 0x%d", ip_hl);
  67. len -= (ip_hl - TCPR_IPV6_H);
  68. } else {
  69. ip_hl = ipv4->ip_hl << 2;
  70. }
  71. switch (proto) {
  72. case IPPROTO_TCP:
  73. if (len < (int)sizeof(tcp_hdr_t)) {
  74. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum TCP with insufficient L4 data");
  75. return TCPEDIT_WARN;
  76. }
  77. tcp = (tcp_hdr_t *)(data + ip_hl);
  78. #ifdef STUPID_SOLARIS_CHECKSUM_BUG
  79. tcp->th_sum = tcp->th_off << 2;
  80. return (TCPEDIT_OK);
  81. #endif
  82. tcp->th_sum = 0;
  83. /* Note, we do both src & dst IP's at the same time, that's why the
  84. * length is 2x a single IP
  85. */
  86. if (ipv6 != NULL) {
  87. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  88. } else {
  89. sum = do_checksum_math((uint16_t *)&ipv4->ip_src, 8);
  90. }
  91. sum += ntohs(IPPROTO_TCP + len);
  92. sum += do_checksum_math((uint16_t *)tcp, len);
  93. tcp->th_sum = CHECKSUM_CARRY(sum);
  94. break;
  95. case IPPROTO_UDP:
  96. if (len < (int)sizeof(udp_hdr_t)) {
  97. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum UDP with insufficient L4 data");
  98. return TCPEDIT_WARN;
  99. }
  100. udp = (udp_hdr_t *)(data + ip_hl);
  101. /* No need to recalculate UDP checksums if already 0 */
  102. if (udp->uh_sum == 0)
  103. break;
  104. udp->uh_sum = 0;
  105. if (ipv6 != NULL) {
  106. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  107. } else {
  108. sum = do_checksum_math((uint16_t *)&ipv4->ip_src, 8);
  109. }
  110. sum += ntohs(IPPROTO_UDP + len);
  111. sum += do_checksum_math((uint16_t *)udp, len);
  112. udp->uh_sum = CHECKSUM_CARRY(sum);
  113. break;
  114. case IPPROTO_ICMP:
  115. if (len < (int)sizeof(icmpv4_hdr_t)) {
  116. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum ICMP with insufficient L4 data");
  117. return TCPEDIT_WARN;
  118. }
  119. icmp = (icmpv4_hdr_t *)(data + ip_hl);
  120. icmp->icmp_sum = 0;
  121. if (ipv6 != NULL) {
  122. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  123. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  124. }
  125. sum += do_checksum_math((uint16_t *)icmp, len);
  126. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  127. break;
  128. case IPPROTO_ICMP6:
  129. if (len < (int)sizeof(icmpv6_hdr_t)) {
  130. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum ICMP6 with insufficient L4 data");
  131. return TCPEDIT_WARN;
  132. }
  133. icmp6 = (icmpv6_hdr_t *)(data + ip_hl);
  134. icmp6->icmp_sum = 0;
  135. if (ipv6 != NULL) {
  136. sum = do_checksum_math((u_int16_t *)&ipv6->ip_src, 32);
  137. }
  138. sum += ntohs(IPPROTO_ICMP6 + len);
  139. sum += do_checksum_math((u_int16_t *)icmp6, len);
  140. icmp6->icmp_sum = CHECKSUM_CARRY(sum);
  141. break;
  142. default:
  143. if (ipv4) {
  144. ipv4->ip_sum = 0;
  145. sum = do_checksum_math((uint16_t *)data, ip_hl);
  146. ipv4->ip_sum = CHECKSUM_CARRY(sum);
  147. } else {
  148. tcpedit_setwarn(tcpedit, "Unsupported protocol for checksum: 0x%x", proto);
  149. return TCPEDIT_WARN;
  150. }
  151. }
  152. return TCPEDIT_OK;
  153. }
  154. /**
  155. * code to do a ones-compliment checksum
  156. */
  157. static int
  158. do_checksum_math(uint16_t *data, int len)
  159. {
  160. int sum = 0;
  161. union {
  162. uint16_t s;
  163. uint8_t b[2];
  164. } pad;
  165. while (len > 1) {
  166. sum += *data++;
  167. len -= 2;
  168. }
  169. if (len == 1) {
  170. pad.b[0] = *(uint8_t *)data;
  171. pad.b[1] = 0;
  172. sum += pad.s;
  173. }
  174. return (sum);
  175. }