checksum.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2011 Daniel Drown
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * checksum.c - ipv4/ipv6 checksum calculation
  17. */
  18. #include <netinet/icmp6.h>
  19. #include <netinet/in.h>
  20. #include <netinet/ip.h>
  21. #include <netinet/ip6.h>
  22. #include <netinet/ip_icmp.h>
  23. #include <netinet/tcp.h>
  24. #include <netinet/udp.h>
  25. #include "netutils/checksum.h"
  26. /* function: ip_checksum_add
  27. * adds data to a checksum. only known to work on little-endian hosts
  28. * current - the current checksum (or 0 to start a new checksum)
  29. * data - the data to add to the checksum
  30. * len - length of data
  31. */
  32. uint32_t ip_checksum_add(uint32_t current, const void* data, int len) {
  33. uint32_t checksum = current;
  34. int left = len;
  35. const uint16_t* data_16 = data;
  36. while (left > 1) {
  37. checksum += *data_16;
  38. data_16++;
  39. left -= 2;
  40. }
  41. if (left) {
  42. checksum += *(uint8_t*)data_16;
  43. }
  44. return checksum;
  45. }
  46. /* function: ip_checksum_fold
  47. * folds a 32-bit partial checksum into 16 bits
  48. * temp_sum - sum from ip_checksum_add
  49. * returns: the folded checksum in network byte order
  50. */
  51. uint16_t ip_checksum_fold(uint32_t temp_sum) {
  52. while (temp_sum > 0xffff) {
  53. temp_sum = (temp_sum >> 16) + (temp_sum & 0xFFFF);
  54. }
  55. return temp_sum;
  56. }
  57. /* function: ip_checksum_finish
  58. * folds and closes the checksum
  59. * temp_sum - sum from ip_checksum_add
  60. * returns: a header checksum value in network byte order
  61. */
  62. uint16_t ip_checksum_finish(uint32_t temp_sum) {
  63. return ~ip_checksum_fold(temp_sum);
  64. }
  65. /* function: ip_checksum
  66. * combined ip_checksum_add and ip_checksum_finish
  67. * data - data to checksum
  68. * len - length of data
  69. */
  70. uint16_t ip_checksum(const void* data, int len) {
  71. // TODO: consider starting from 0xffff so the checksum of a buffer entirely consisting of zeros
  72. // is correctly calculated as 0.
  73. uint32_t temp_sum;
  74. temp_sum = ip_checksum_add(0, data, len);
  75. return ip_checksum_finish(temp_sum);
  76. }
  77. /* function: ipv6_pseudo_header_checksum
  78. * calculate the pseudo header checksum for use in tcp/udp/icmp headers
  79. * ip6 - the ipv6 header
  80. * len - the transport length (transport header + payload)
  81. * protocol - the transport layer protocol, can be different from ip6->ip6_nxt for fragments
  82. */
  83. uint32_t ipv6_pseudo_header_checksum(const struct ip6_hdr* ip6, uint32_t len, uint8_t protocol) {
  84. uint32_t checksum_len = htonl(len);
  85. uint32_t checksum_next = htonl(protocol);
  86. uint32_t current = 0;
  87. current = ip_checksum_add(current, &(ip6->ip6_src), sizeof(struct in6_addr));
  88. current = ip_checksum_add(current, &(ip6->ip6_dst), sizeof(struct in6_addr));
  89. current = ip_checksum_add(current, &checksum_len, sizeof(checksum_len));
  90. current = ip_checksum_add(current, &checksum_next, sizeof(checksum_next));
  91. return current;
  92. }
  93. /* function: ipv4_pseudo_header_checksum
  94. * calculate the pseudo header checksum for use in tcp/udp headers
  95. * ip - the ipv4 header
  96. * len - the transport length (transport header + payload)
  97. */
  98. uint32_t ipv4_pseudo_header_checksum(const struct iphdr* ip, uint16_t len) {
  99. uint16_t temp_protocol, temp_length;
  100. temp_protocol = htons(ip->protocol);
  101. temp_length = htons(len);
  102. uint32_t current = 0;
  103. current = ip_checksum_add(current, &(ip->saddr), sizeof(uint32_t));
  104. current = ip_checksum_add(current, &(ip->daddr), sizeof(uint32_t));
  105. current = ip_checksum_add(current, &temp_protocol, sizeof(uint16_t));
  106. current = ip_checksum_add(current, &temp_length, sizeof(uint16_t));
  107. return current;
  108. }
  109. /* function: ip_checksum_adjust
  110. * calculates a new checksum given a previous checksum and the old and new pseudo-header checksums
  111. * checksum - the header checksum in the original packet in network byte order
  112. * old_hdr_sum - the pseudo-header checksum of the original packet
  113. * new_hdr_sum - the pseudo-header checksum of the translated packet
  114. * returns: the new header checksum in network byte order
  115. */
  116. uint16_t ip_checksum_adjust(uint16_t checksum, uint32_t old_hdr_sum, uint32_t new_hdr_sum) {
  117. // Algorithm suggested in RFC 1624.
  118. // http://tools.ietf.org/html/rfc1624#section-3
  119. checksum = ~checksum;
  120. uint16_t folded_sum = ip_checksum_fold(checksum + new_hdr_sum);
  121. uint16_t folded_old = ip_checksum_fold(old_hdr_sum);
  122. if (folded_sum > folded_old) {
  123. return ~(folded_sum - folded_old);
  124. } else {
  125. return ~(folded_sum - folded_old - 1); // end-around borrow
  126. }
  127. }