checksum.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* TODO: csum_tcpudp_magic could be speeded up, and csum_fold as well */
  2. #ifndef _CRIS_CHECKSUM_H
  3. #define _CRIS_CHECKSUM_H
  4. #include <asm/arch/checksum.h>
  5. /*
  6. * computes the checksum of a memory block at buff, length len,
  7. * and adds in "sum" (32-bit)
  8. *
  9. * returns a 32-bit number suitable for feeding into itself
  10. * or csum_tcpudp_magic
  11. *
  12. * this function must be called with even lengths, except
  13. * for the last fragment, which may be odd
  14. *
  15. * it's best to have buff aligned on a 32-bit boundary
  16. */
  17. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
  18. /*
  19. * the same as csum_partial, but copies from src while it
  20. * checksums
  21. *
  22. * here even more important to align src and dst on a 32-bit (or even
  23. * better 64-bit) boundary
  24. */
  25. unsigned int csum_partial_copy_nocheck(const char *src, char *dst,
  26. int len, unsigned int sum);
  27. /*
  28. * Fold a partial checksum into a word
  29. */
  30. extern inline unsigned int csum_fold(unsigned int sum)
  31. {
  32. /* the while loop is unnecessary really, it's always enough with two
  33. iterations */
  34. while(sum >> 16)
  35. sum = (sum & 0xffff) + (sum >> 16); /* add in end-around carry */
  36. return ~sum;
  37. }
  38. extern unsigned int csum_partial_copy_from_user(const char *src, char *dst,
  39. int len, unsigned int sum,
  40. int *errptr);
  41. /*
  42. * This is a version of ip_compute_csum() optimized for IP headers,
  43. * which always checksum on 4 octet boundaries.
  44. *
  45. */
  46. extern inline unsigned short ip_fast_csum(unsigned char * iph,
  47. unsigned int ihl)
  48. {
  49. return csum_fold(csum_partial(iph, ihl * 4, 0));
  50. }
  51. /*
  52. * computes the checksum of the TCP/UDP pseudo-header
  53. * returns a 16-bit checksum, already complemented
  54. */
  55. extern inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  56. unsigned long daddr,
  57. unsigned short len,
  58. unsigned short proto,
  59. unsigned int sum)
  60. {
  61. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  62. }
  63. /*
  64. * this routine is used for miscellaneous IP-like checksums, mainly
  65. * in icmp.c
  66. */
  67. extern inline unsigned short ip_compute_csum(unsigned char * buff, int len) {
  68. return csum_fold (csum_partial(buff, len, 0));
  69. }
  70. #endif