checksum.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef _M68K_CHECKSUM_H
  2. #define _M68K_CHECKSUM_H
  3. #include <linux/in6.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. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
  17. /*
  18. * the same as csum_partial, but copies from src while it
  19. * checksums
  20. *
  21. * here even more important to align src and dst on a 32-bit (or even
  22. * better 64-bit) boundary
  23. */
  24. extern unsigned int csum_partial_copy_from_user(const unsigned char __user *src,
  25. unsigned char *dst,
  26. int len, int sum,
  27. int *csum_err);
  28. extern unsigned int csum_partial_copy_nocheck(const unsigned char *src,
  29. unsigned char *dst, int len,
  30. int sum);
  31. /*
  32. * This is a version of ip_compute_csum() optimized for IP headers,
  33. * which always checksum on 4 octet boundaries.
  34. *
  35. */
  36. static inline unsigned short
  37. ip_fast_csum(unsigned char *iph, unsigned int ihl)
  38. {
  39. unsigned int sum = 0;
  40. unsigned long tmp;
  41. __asm__ ("subqw #1,%2\n"
  42. "1:\t"
  43. "movel %1@+,%3\n\t"
  44. "addxl %3,%0\n\t"
  45. "dbra %2,1b\n\t"
  46. "movel %0,%3\n\t"
  47. "swap %3\n\t"
  48. "addxw %3,%0\n\t"
  49. "clrw %3\n\t"
  50. "addxw %3,%0\n\t"
  51. : "=d" (sum), "=&a" (iph), "=&d" (ihl), "=&d" (tmp)
  52. : "0" (sum), "1" (iph), "2" (ihl)
  53. : "memory");
  54. return ~sum;
  55. }
  56. /*
  57. * Fold a partial checksum
  58. */
  59. static inline unsigned int csum_fold(unsigned int sum)
  60. {
  61. unsigned int tmp = sum;
  62. __asm__("swap %1\n\t"
  63. "addw %1, %0\n\t"
  64. "clrw %1\n\t"
  65. "addxw %1, %0"
  66. : "=&d" (sum), "=&d" (tmp)
  67. : "0" (sum), "1" (tmp));
  68. return ~sum;
  69. }
  70. static inline unsigned int
  71. csum_tcpudp_nofold(unsigned long saddr, unsigned long daddr, unsigned short len,
  72. unsigned short proto, unsigned int sum)
  73. {
  74. __asm__ ("addl %2,%0\n\t"
  75. "addxl %3,%0\n\t"
  76. "addxl %4,%0\n\t"
  77. "clrl %1\n\t"
  78. "addxl %1,%0"
  79. : "=&d" (sum), "=d" (saddr)
  80. : "g" (daddr), "1" (saddr), "d" (len + proto),
  81. "0" (sum));
  82. return sum;
  83. }
  84. /*
  85. * computes the checksum of the TCP/UDP pseudo-header
  86. * returns a 16-bit checksum, already complemented
  87. */
  88. static inline unsigned short int
  89. csum_tcpudp_magic(unsigned long saddr, unsigned long daddr, unsigned short len,
  90. unsigned short proto, unsigned int sum)
  91. {
  92. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  93. }
  94. /*
  95. * this routine is used for miscellaneous IP-like checksums, mainly
  96. * in icmp.c
  97. */
  98. static inline unsigned short
  99. ip_compute_csum(unsigned char * buff, int len)
  100. {
  101. return csum_fold (csum_partial(buff, len, 0));
  102. }
  103. #define _HAVE_ARCH_IPV6_CSUM
  104. static __inline__ unsigned short int
  105. csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
  106. __u32 len, unsigned short proto, unsigned int sum)
  107. {
  108. register unsigned long tmp;
  109. __asm__("addl %2@,%0\n\t"
  110. "movel %2@(4),%1\n\t"
  111. "addxl %1,%0\n\t"
  112. "movel %2@(8),%1\n\t"
  113. "addxl %1,%0\n\t"
  114. "movel %2@(12),%1\n\t"
  115. "addxl %1,%0\n\t"
  116. "movel %3@,%1\n\t"
  117. "addxl %1,%0\n\t"
  118. "movel %3@(4),%1\n\t"
  119. "addxl %1,%0\n\t"
  120. "movel %3@(8),%1\n\t"
  121. "addxl %1,%0\n\t"
  122. "movel %3@(12),%1\n\t"
  123. "addxl %1,%0\n\t"
  124. "addxl %4,%0\n\t"
  125. "clrl %1\n\t"
  126. "addxl %1,%0"
  127. : "=&d" (sum), "=&d" (tmp)
  128. : "a" (saddr), "a" (daddr), "d" (len + proto),
  129. "0" (sum));
  130. return csum_fold(sum);
  131. }
  132. #endif /* _M68K_CHECKSUM_H */