2
0

net-checksum.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * IP checksumming functions.
  3. * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; under version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  17. */
  18. #include "hw/hw.h"
  19. #include "net.h"
  20. #define PROTO_TCP 6
  21. #define PROTO_UDP 17
  22. uint32_t net_checksum_add(int len, uint8_t *buf)
  23. {
  24. uint32_t sum = 0;
  25. int i;
  26. for (i = 0; i < len; i++) {
  27. if (i & 1)
  28. sum += (uint32_t)buf[i];
  29. else
  30. sum += (uint32_t)buf[i] << 8;
  31. }
  32. return sum;
  33. }
  34. uint16_t net_checksum_finish(uint32_t sum)
  35. {
  36. while (sum>>16)
  37. sum = (sum & 0xFFFF)+(sum >> 16);
  38. return ~sum;
  39. }
  40. uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
  41. uint8_t *addrs, uint8_t *buf)
  42. {
  43. uint32_t sum = 0;
  44. sum += net_checksum_add(length, buf); // payload
  45. sum += net_checksum_add(8, addrs); // src + dst address
  46. sum += proto + length; // protocol & length
  47. return net_checksum_finish(sum);
  48. }
  49. void net_checksum_calculate(uint8_t *data, int length)
  50. {
  51. int hlen, plen, proto, csum_offset;
  52. uint16_t csum;
  53. if ((data[14] & 0xf0) != 0x40)
  54. return; /* not IPv4 */
  55. hlen = (data[14] & 0x0f) * 4;
  56. plen = (data[16] << 8 | data[17]) - hlen;
  57. proto = data[23];
  58. switch (proto) {
  59. case PROTO_TCP:
  60. csum_offset = 16;
  61. break;
  62. case PROTO_UDP:
  63. csum_offset = 6;
  64. break;
  65. default:
  66. return;
  67. }
  68. if (plen < csum_offset+2)
  69. return;
  70. data[14+hlen+csum_offset] = 0;
  71. data[14+hlen+csum_offset+1] = 0;
  72. csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
  73. data[14+hlen+csum_offset] = csum >> 8;
  74. data[14+hlen+csum_offset+1] = csum & 0xff;
  75. }