checksum.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 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 || len <= 0 || len > 65535) {
  46. tcpedit_setwarn(tcpedit, "%s", "Unable to checksum packets with no L3+ data");
  47. return TCPEDIT_WARN;
  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, len);
  54. dbgx(3, "layer4 proto is 0x%hx", (uint16_t)proto);
  55. layer = (u_char*)get_layer4_v6(ipv6, len);
  56. if (!layer) {
  57. tcpedit_setwarn(tcpedit, "%s", "Packet to short for checksum");
  58. return TCPEDIT_WARN;
  59. }
  60. ip_hl = 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. tcp = (tcp_hdr_t *)(data + ip_hl);
  69. #ifdef STUPID_SOLARIS_CHECKSUM_BUG
  70. tcp->th_sum = tcp->th_off << 2;
  71. return (TCPEDIT_OK);
  72. #endif
  73. tcp->th_sum = 0;
  74. /* Note, we do both src & dst IP's at the same time, that's why the
  75. * length is 2x a single IP
  76. */
  77. if (ipv6 != NULL) {
  78. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  79. } else {
  80. sum = do_checksum_math((uint16_t *)&ipv4->ip_src, 8);
  81. }
  82. sum += ntohs(IPPROTO_TCP + len);
  83. sum += do_checksum_math((uint16_t *)tcp, len);
  84. tcp->th_sum = CHECKSUM_CARRY(sum);
  85. break;
  86. case IPPROTO_UDP:
  87. udp = (udp_hdr_t *)(data + ip_hl);
  88. /* No need to recalculate UDP checksums if already 0 */
  89. if (udp->uh_sum == 0)
  90. break;
  91. udp->uh_sum = 0;
  92. if (ipv6 != NULL) {
  93. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  94. } else {
  95. sum = do_checksum_math((uint16_t *)&ipv4->ip_src, 8);
  96. }
  97. sum += ntohs(IPPROTO_UDP + len);
  98. sum += do_checksum_math((uint16_t *)udp, len);
  99. udp->uh_sum = CHECKSUM_CARRY(sum);
  100. break;
  101. case IPPROTO_ICMP:
  102. icmp = (icmpv4_hdr_t *)(data + ip_hl);
  103. icmp->icmp_sum = 0;
  104. if (ipv6 != NULL) {
  105. sum = do_checksum_math((uint16_t *)&ipv6->ip_src, 32);
  106. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  107. }
  108. sum += do_checksum_math((uint16_t *)icmp, len);
  109. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  110. break;
  111. case IPPROTO_ICMP6:
  112. icmp6 = (icmpv6_hdr_t *)(data + ip_hl);
  113. icmp6->icmp_sum = 0;
  114. if (ipv6 != NULL) {
  115. sum = do_checksum_math((u_int16_t *)&ipv6->ip_src, 32);
  116. }
  117. sum += ntohs(IPPROTO_ICMP6 + len);
  118. sum += do_checksum_math((u_int16_t *)icmp6, len);
  119. icmp6->icmp_sum = CHECKSUM_CARRY(sum);
  120. break;
  121. case IPPROTO_IP:
  122. if (ipv4) {
  123. ipv4->ip_sum = 0;
  124. sum = do_checksum_math((uint16_t *)data, ip_hl);
  125. ipv4->ip_sum = CHECKSUM_CARRY(sum);
  126. }
  127. break;
  128. case IPPROTO_IGMP:
  129. case IPPROTO_GRE:
  130. case IPPROTO_OSPF:
  131. case IPPROTO_OSPF_LSA:
  132. case IPPROTO_VRRP:
  133. case TCPR_PROTO_CDP:
  134. case TCPR_PROTO_ISL:
  135. default:
  136. tcpedit_setwarn(tcpedit, "Unsupported protocol for checksum: 0x%x", proto);
  137. return TCPEDIT_WARN;
  138. }
  139. return TCPEDIT_OK;
  140. }
  141. /**
  142. * code to do a ones-compliment checksum
  143. */
  144. static int
  145. do_checksum_math(uint16_t *data, int len)
  146. {
  147. int sum = 0;
  148. union {
  149. uint16_t s;
  150. uint8_t b[2];
  151. } pad;
  152. while (len > 1) {
  153. sum += *data++;
  154. len -= 2;
  155. }
  156. if (len == 1) {
  157. pad.b[0] = *(uint8_t *)data;
  158. pad.b[1] = 0;
  159. sum += pad.s;
  160. }
  161. return (sum);
  162. }