checksum.c 5.8 KB

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