checksum.c 5.2 KB

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