checksum.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2024 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 "checksum.h"
  24. #include "config.h"
  25. static int do_checksum_math(uint16_t *, int);
  26. /**
  27. * Returns -1 on error and 0 on success, 1 on warn
  28. */
  29. int
  30. do_checksum(tcpedit_t *tcpedit, uint8_t *data, int proto, int len, const u_char *end_ptr)
  31. {
  32. ipv4_hdr_t *ipv4;
  33. ipv6_hdr_t *ipv6;
  34. tcp_hdr_t *tcp;
  35. udp_hdr_t *udp;
  36. icmpv4_hdr_t *icmp;
  37. icmpv6_hdr_t *icmp6;
  38. u_char *layer;
  39. int ip_hl;
  40. int sum;
  41. sum = 0;
  42. ipv4 = NULL;
  43. ipv6 = NULL;
  44. assert(data);
  45. if (!data || len <= 0) {
  46. tcpedit_seterr(tcpedit, "%s", "length of data must be > 0");
  47. return TCPEDIT_ERROR;
  48. }
  49. ipv4 = (ipv4_hdr_t *)data;
  50. if (ipv4->ip_v == 6) {
  51. ipv6 = (ipv6_hdr_t *)data;
  52. ipv4 = NULL;
  53. proto = get_ipv6_l4proto(ipv6, end_ptr);
  54. dbgx(3, "layer4 proto is 0x%hx", (uint16_t)proto);
  55. layer = (u_char *)get_layer4_v6(ipv6, end_ptr);
  56. if (!layer) {
  57. tcpedit_setwarn(tcpedit, "%s", "Packet to short for checksum");
  58. return TCPEDIT_WARN;
  59. }
  60. ip_hl = (int)(layer - (u_char *)data);
  61. dbgx(3, "ip_hl proto is 0x%d", ip_hl);
  62. len -= (ip_hl - TCPR_IPV6_H);
  63. } else {
  64. ip_hl = ipv4->ip_hl << 2;
  65. }
  66. switch (proto) {
  67. case IPPROTO_TCP:
  68. if (len < (int)sizeof(tcp_hdr_t)) {
  69. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum TCP with insufficient L4 data");
  70. return TCPEDIT_WARN;
  71. }
  72. tcp = (tcp_hdr_t *)(data + ip_hl);
  73. #ifdef STUPID_SOLARIS_CHECKSUM_BUG
  74. tcp->th_sum = tcp->th_off << 2;
  75. return (TCPEDIT_OK);
  76. #endif
  77. tcp->th_sum = 0;
  78. /* Note, we do both src & dst IP's at the same time, that's why the
  79. * length is 2x a single IP
  80. */
  81. if (ipv6 != NULL) {
  82. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  83. } else {
  84. sum = do_checksum_math((uint16_t *)&ipv4->ip_src, 8);
  85. }
  86. sum += ntohs(IPPROTO_TCP + len);
  87. sum += do_checksum_math((uint16_t *)tcp, len);
  88. tcp->th_sum = CHECKSUM_CARRY(sum);
  89. break;
  90. case IPPROTO_UDP:
  91. if (len < (int)sizeof(udp_hdr_t)) {
  92. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum UDP with insufficient L4 data");
  93. return TCPEDIT_WARN;
  94. }
  95. udp = (udp_hdr_t *)(data + ip_hl);
  96. /* No need to recalculate UDP checksums if already 0 */
  97. if (udp->uh_sum == 0)
  98. break;
  99. udp->uh_sum = 0;
  100. if (ipv6 != NULL) {
  101. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  102. } else {
  103. sum = do_checksum_math((uint16_t *)&ipv4->ip_src, 8);
  104. }
  105. sum += ntohs(IPPROTO_UDP + len);
  106. sum += do_checksum_math((uint16_t *)udp, len);
  107. udp->uh_sum = CHECKSUM_CARRY(sum);
  108. break;
  109. case IPPROTO_ICMP:
  110. if (len < (int)sizeof(icmpv4_hdr_t)) {
  111. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum ICMP with insufficient L4 data");
  112. return TCPEDIT_WARN;
  113. }
  114. icmp = (icmpv4_hdr_t *)(data + ip_hl);
  115. icmp->icmp_sum = 0;
  116. if (ipv6 != NULL) {
  117. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  118. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  119. }
  120. sum += do_checksum_math((uint16_t *)icmp, len);
  121. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  122. break;
  123. case IPPROTO_ICMP6:
  124. if (len < (int)sizeof(icmpv6_hdr_t)) {
  125. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum ICMP6 with insufficient L4 data");
  126. return TCPEDIT_WARN;
  127. }
  128. icmp6 = (icmpv6_hdr_t *)(data + ip_hl);
  129. icmp6->icmp_sum = 0;
  130. if (ipv6 != NULL) {
  131. sum = do_checksum_math((u_int16_t *)&ipv6->ip_src, 32);
  132. }
  133. sum += ntohs(IPPROTO_ICMP6 + len);
  134. sum += do_checksum_math((u_int16_t *)icmp6, len);
  135. icmp6->icmp_sum = CHECKSUM_CARRY(sum);
  136. break;
  137. default:
  138. if (ipv4) {
  139. ipv4->ip_sum = 0;
  140. sum = do_checksum_math((uint16_t *)data, ip_hl);
  141. ipv4->ip_sum = CHECKSUM_CARRY(sum);
  142. } else {
  143. tcpedit_setwarn(tcpedit, "Unsupported protocol for checksum: 0x%x", proto);
  144. return TCPEDIT_WARN;
  145. }
  146. }
  147. return TCPEDIT_OK;
  148. }
  149. /**
  150. * code to do a ones-compliment checksum
  151. */
  152. static int
  153. do_checksum_math(uint16_t *data, int len)
  154. {
  155. int sum = 0;
  156. union {
  157. uint16_t s;
  158. uint8_t b[2];
  159. } pad;
  160. while (len > 1) {
  161. sum += *data++;
  162. len -= 2;
  163. }
  164. if (len == 1) {
  165. pad.b[0] = *(uint8_t *)data;
  166. pad.b[1] = 0;
  167. sum += pad.s;
  168. }
  169. return (sum);
  170. }