checksum.c 6.5 KB

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