checksum.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifdef __KERNEL__
  2. #ifndef _PPC_CHECKSUM_H
  3. #define _PPC_CHECKSUM_H
  4. /*
  5. * computes the checksum of a memory block at buff, length len,
  6. * and adds in "sum" (32-bit)
  7. *
  8. * returns a 32-bit number suitable for feeding into itself
  9. * or csum_tcpudp_magic
  10. *
  11. * this function must be called with even lengths, except
  12. * for the last fragment, which may be odd
  13. *
  14. * it's best to have buff aligned on a 32-bit boundary
  15. */
  16. extern unsigned int csum_partial(const unsigned char * buff, int len,
  17. unsigned int sum);
  18. /*
  19. * Computes the checksum of a memory block at src, length len,
  20. * and adds in "sum" (32-bit), while copying the block to dst.
  21. * If an access exception occurs on src or dst, it stores -EFAULT
  22. * to *src_err or *dst_err respectively (if that pointer is not
  23. * NULL), and, for an error on src, zeroes the rest of dst.
  24. *
  25. * Like csum_partial, this must be called with even lengths,
  26. * except for the last fragment.
  27. */
  28. extern unsigned int csum_partial_copy_generic(const char *src, char *dst,
  29. int len, unsigned int sum,
  30. int *src_err, int *dst_err);
  31. #define csum_partial_copy_from_user(src, dst, len, sum, errp) \
  32. csum_partial_copy_generic((__force void *)(src), (dst), (len), (sum), (errp), NULL)
  33. /* FIXME: this needs to be written to really do no check -- Cort */
  34. #define csum_partial_copy_nocheck(src, dst, len, sum) \
  35. csum_partial_copy_generic((src), (dst), (len), (sum), NULL, NULL)
  36. /*
  37. * turns a 32-bit partial checksum (e.g. from csum_partial) into a
  38. * 1's complement 16-bit checksum.
  39. */
  40. static inline unsigned int csum_fold(unsigned int sum)
  41. {
  42. unsigned int tmp;
  43. /* swap the two 16-bit halves of sum */
  44. __asm__("rlwinm %0,%1,16,0,31" : "=r" (tmp) : "r" (sum));
  45. /* if there is a carry from adding the two 16-bit halves,
  46. it will carry from the lower half into the upper half,
  47. giving us the correct sum in the upper half. */
  48. sum = ~(sum + tmp) >> 16;
  49. return sum;
  50. }
  51. /*
  52. * this routine is used for miscellaneous IP-like checksums, mainly
  53. * in icmp.c
  54. */
  55. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  56. {
  57. return csum_fold(csum_partial(buff, len, 0));
  58. }
  59. /*
  60. * FIXME: I swiped this one from the sparc and made minor modifications.
  61. * It may not be correct. -- Cort
  62. */
  63. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  64. unsigned long daddr,
  65. unsigned short len,
  66. unsigned short proto,
  67. unsigned int sum)
  68. {
  69. __asm__("\n\
  70. addc %0,%0,%1 \n\
  71. adde %0,%0,%2 \n\
  72. adde %0,%0,%3 \n\
  73. addze %0,%0 \n\
  74. "
  75. : "=r" (sum)
  76. : "r" (daddr), "r"(saddr), "r"((proto<<16)+len), "0"(sum));
  77. return sum;
  78. }
  79. /*
  80. * This is a version of ip_compute_csum() optimized for IP headers,
  81. * which always checksum on 4 octet boundaries. ihl is the number
  82. * of 32-bit words and is always >= 5.
  83. */
  84. extern unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl);
  85. /*
  86. * computes the checksum of the TCP/UDP pseudo-header
  87. * returns a 16-bit checksum, already complemented
  88. */
  89. extern unsigned short csum_tcpudp_magic(unsigned long saddr,
  90. unsigned long daddr,
  91. unsigned short len,
  92. unsigned short proto,
  93. unsigned int sum);
  94. #endif
  95. #endif /* __KERNEL__ */