checksum.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* $Id: checksum.c 1895 2007-08-22 04:24:33Z aturner $ */
  2. /*
  3. * Copyright (c) 2006 Aaron Turner.
  4. * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright owners nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  28. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. * This code is heavily based on (some might even say stolen from) Mike Shiffman's
  34. * checksumming code from Libnet 1.1.3
  35. */
  36. #include "config.h"
  37. #include "tcpedit-int.h"
  38. #include "checksum.h"
  39. static int do_checksum_math(u_int16_t *, int);
  40. /**
  41. * Returns -1 on error and 0 on success, 1 on warn
  42. */
  43. int
  44. do_checksum(tcpedit_t *tcpedit, u_int8_t *data, int proto, int len) {
  45. ipv4_hdr_t *ipv4;
  46. ipv6_hdr_t *ipv6;
  47. tcp_hdr_t *tcp;
  48. udp_hdr_t *udp;
  49. icmpv4_hdr_t *icmp;
  50. int ip_hl;
  51. int sum;
  52. sum = 0;
  53. ipv4 = NULL;
  54. ipv6 = NULL;
  55. assert(data);
  56. if (len <= 0) {
  57. tcpedit_seterr(tcpedit, "%s", "length of data must be > 0");
  58. return TCPEDIT_ERROR;
  59. }
  60. ipv4 = (ipv4_hdr_t *)data;
  61. if (ipv4->ip_v == 6) {
  62. ipv6 = (ipv6_hdr_t *)data;
  63. ipv4 = NULL;
  64. ip_hl = 40;
  65. } else {
  66. ip_hl = ipv4->ip_hl << 2;
  67. }
  68. switch (proto) {
  69. case IPPROTO_TCP:
  70. tcp = (tcp_hdr_t *)(data + ip_hl);
  71. #ifdef STUPID_SOLARIS_CHECKSUM_BUG
  72. tcp->th_sum = tcp->th_off << 2;
  73. return (TCPEDIT_OK);
  74. #endif
  75. tcp->th_sum = 0;
  76. /* Note, we do both src & dst IP's at the same time, that's why the
  77. * length is 2x a single IP
  78. */
  79. if (ipv6 != NULL) {
  80. sum = do_checksum_math((u_int16_t *)&ipv6->ip_src, 32);
  81. } else {
  82. sum = do_checksum_math((u_int16_t *)&ipv4->ip_src, 8);
  83. }
  84. sum += ntohs(IPPROTO_TCP + len);
  85. sum += do_checksum_math((u_int16_t *)tcp, len);
  86. tcp->th_sum = CHECKSUM_CARRY(sum);
  87. break;
  88. case IPPROTO_UDP:
  89. udp = (udp_hdr_t *)(data + ip_hl);
  90. udp->uh_sum = 0;
  91. if (ipv6 != NULL) {
  92. sum = do_checksum_math((u_int16_t *)&ipv6->ip_src, 32);
  93. } else {
  94. sum = do_checksum_math((u_int16_t *)&ipv4->ip_src, 8);
  95. }
  96. sum += ntohs(IPPROTO_UDP + len);
  97. sum += do_checksum_math((u_int16_t *)udp, len);
  98. udp->uh_sum = CHECKSUM_CARRY(sum);
  99. break;
  100. case IPPROTO_ICMP:
  101. icmp = (icmpv4_hdr_t *)(data + ip_hl);
  102. icmp->icmp_sum = 0;
  103. if (ipv6 != NULL) {
  104. sum = do_checksum_math((u_int16_t *)&ipv6->ip_src, 32);
  105. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  106. }
  107. sum += do_checksum_math((u_int16_t *)icmp, len);
  108. icmp->icmp_sum = CHECKSUM_CARRY(sum);
  109. break;
  110. case IPPROTO_IP:
  111. ipv4->ip_sum = 0;
  112. sum = do_checksum_math((u_int16_t *)data, ip_hl);
  113. ipv4->ip_sum = CHECKSUM_CARRY(sum);
  114. break;
  115. case IPPROTO_IGMP:
  116. case IPPROTO_GRE:
  117. case IPPROTO_OSPF:
  118. case IPPROTO_OSPF_LSA:
  119. case IPPROTO_VRRP:
  120. case TCPR_PROTO_CDP:
  121. case TCPR_PROTO_ISL:
  122. default:
  123. tcpedit_setwarn(tcpedit, "Unsupported protocol for checksum: 0x%x", proto);
  124. return TCPEDIT_WARN;
  125. }
  126. return TCPEDIT_OK;
  127. }
  128. /**
  129. * code to do a ones-compliment checksum
  130. */
  131. static int
  132. do_checksum_math(u_int16_t *data, int len)
  133. {
  134. int sum = 0;
  135. union {
  136. u_int16_t s;
  137. u_int8_t b[2];
  138. } pad;
  139. while (len > 1) {
  140. sum += *data++;
  141. len -= 2;
  142. }
  143. if (len == 1) {
  144. pad.b[0] = *(u_int8_t *)data;
  145. pad.b[1] = 0;
  146. sum += pad.s;
  147. }
  148. return (sum);
  149. }