checksum.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * INET An implementation of the TCP/IP protocol suite for the LINUX
  3.  * operating system.  INET is implemented using the  BSD Socket
  4.  * interface as the means of communication with the user level.
  5.  *
  6.  * IP/TCP/UDP checksumming routines
  7.  *
  8.  * Authors: Jorge Cwik, <jorge@laser.satlink.net>
  9.  * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  10.  * Tom May, <ftom@netcom.com>
  11.  * Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
  12.  * Lots of code moved from tcp.c and ip.c; see those files
  13.  * for more names.
  14.  *
  15.  * 03/02/96 Jes Sorensen, Andreas Schwab, Roman Hodek:
  16.  * Fixed some nasty bugs, causing some horrible crashes.
  17.  * A: At some points, the sum (%0) was used as
  18.  * length-counter instead of the length counter
  19.  * (%1). Thanks to Roman Hodek for pointing this out.
  20.  * B: GCC seems to mess up if one uses too many
  21.  * data-registers to hold input values and one tries to
  22.  * specify d0 and d1 as scratch registers. Letting gcc
  23.  * choose these registers itself solves the problem.
  24.  *
  25.  * This program is free software; you can redistribute it and/or
  26.  * modify it under the terms of the GNU General Public License
  27.  * as published by the Free Software Foundation; either version
  28.  * 2 of the License, or (at your option) any later version.
  29.  *
  30.  * 1998/8/31 Andreas Schwab:
  31.  * Zero out rest of buffer on exception in
  32.  * csum_partial_copy_from_user.
  33.  */
  34. #include <net/checksum.h>
  35. /*
  36.  * computes a partial checksum, e.g. for TCP/UDP fragments
  37.  */
  38. unsigned int
  39. csum_partial (const unsigned char *buff, int len, unsigned int sum)
  40. {
  41. unsigned long tmp1, tmp2;
  42.   /*
  43.    * Experiments with ethernet and slip connections show that buff
  44.    * is aligned on either a 2-byte or 4-byte boundary.
  45.    */
  46. __asm__("movel %2,%3nt"
  47. "btst #1,%3nt" /* Check alignment */
  48. "jeq 2fnt"
  49. "subql #2,%1nt" /* buff%4==2: treat first word */
  50. "jgt 1fnt"
  51. "addql #2,%1nt" /* len was == 2, treat only rest */
  52. "jra 4fn"
  53.      "1:t"
  54. "addw %2@+,%0nt" /* add first word to sum */
  55. "clrl %3nt"
  56. "addxl %3,%0n" /* add X bit */
  57.      "2:t"
  58. /* unrolled loop for the main part: do 8 longs at once */
  59. "movel %1,%3nt" /* save len in tmp1 */
  60. "lsrl #5,%1nt" /* len/32 */
  61. "jeq 2fnt" /* not enough... */
  62. "subql #1,%1n"
  63.      "1:t"
  64. "movel %2@+,%4nt"
  65. "addxl %4,%0nt"
  66. "movel %2@+,%4nt"
  67. "addxl %4,%0nt"
  68. "movel %2@+,%4nt"
  69. "addxl %4,%0nt"
  70. "movel %2@+,%4nt"
  71. "addxl %4,%0nt"
  72. "movel %2@+,%4nt"
  73. "addxl %4,%0nt"
  74. "movel %2@+,%4nt"
  75. "addxl %4,%0nt"
  76. "movel %2@+,%4nt"
  77. "addxl %4,%0nt"
  78. "movel %2@+,%4nt"
  79. "addxl %4,%0nt"
  80. "dbra %1,1bnt"
  81. "clrl %4nt"
  82. "addxl %4,%0nt" /* add X bit */
  83. "clrw %1nt"
  84. "subql #1,%1nt"
  85. "jcc 1bn"
  86.      "2:t"
  87. "movel %3,%1nt" /* restore len from tmp1 */
  88. "andw #0x1c,%3nt" /* number of rest longs */
  89. "jeq 4fnt"
  90. "lsrw #2,%3nt"
  91. "subqw #1,%3n"
  92.      "3:t"
  93. /* loop for rest longs */
  94. "movel %2@+,%4nt"
  95. "addxl %4,%0nt"
  96. "dbra %3,3bnt"
  97. "clrl %4nt"
  98. "addxl %4,%0n" /* add X bit */
  99.      "4:t"
  100. /* now check for rest bytes that do not fit into longs */
  101. "andw #3,%1nt"
  102. "jeq 7fnt"
  103. "clrl %4nt" /* clear tmp2 for rest bytes */
  104. "subqw #2,%1nt"
  105. "jlt 5fnt"
  106. "movew %2@+,%4nt" /* have rest >= 2: get word */
  107. "swap %4nt" /* into bits 16..31 */
  108. "tstw %1nt" /* another byte? */
  109. "jeq 6fn"
  110.      "5:t"
  111. "moveb %2@,%4nt" /* have odd rest: get byte */
  112. "lslw #8,%4nt" /* into bits 8..15; 16..31 untouched */
  113.      "6:t"
  114. "addl %4,%0nt" /* now add rest long to sum */
  115. "clrl %4nt"
  116. "addxl %4,%0n" /* add X bit */
  117.      "7:t"
  118. : "=d" (sum), "=d" (len), "=a" (buff),
  119.   "=&d" (tmp1), "=&d" (tmp2)
  120. : "0" (sum), "1" (len), "2" (buff)
  121.     );
  122. return(sum);
  123. }
  124. /*
  125.  * copy from user space while checksumming, with exception handling.
  126.  */
  127. unsigned int
  128. csum_partial_copy_from_user(const char *src, char *dst, int len,
  129.     int sum, int *csum_err)
  130. {
  131. /*
  132.  * GCC doesn't like more than 10 operands for the asm
  133.  * statements so we have to use tmp2 for the error
  134.  * code.
  135.  */
  136. unsigned long tmp1, tmp2;
  137. __asm__("movel %2,%4nt"
  138. "btst #1,%4nt" /* Check alignment */
  139. "jeq 2fnt"
  140. "subql #2,%1nt" /* buff%4==2: treat first word */
  141. "jgt 1fnt"
  142. "addql #2,%1nt" /* len was == 2, treat only rest */
  143. "jra 4fn"
  144.      "1:n"
  145.      "10:t"
  146. "movesw %2@+,%4nt" /* add first word to sum */
  147. "addw %4,%0nt"
  148. "movew %4,%3@+nt"
  149. "clrl %4nt"
  150. "addxl %4,%0n" /* add X bit */
  151.      "2:t"
  152. /* unrolled loop for the main part: do 8 longs at once */
  153. "movel %1,%4nt" /* save len in tmp1 */
  154. "lsrl #5,%1nt" /* len/32 */
  155. "jeq 2fnt" /* not enough... */
  156. "subql #1,%1n"
  157.      "1:n"
  158.      "11:t"
  159. "movesl %2@+,%5nt"
  160. "addxl %5,%0nt"
  161. "movel %5,%3@+nt"
  162.      "12:t"
  163. "movesl %2@+,%5nt"
  164. "addxl %5,%0nt"
  165. "movel %5,%3@+nt"
  166.      "13:t"
  167. "movesl %2@+,%5nt"
  168. "addxl %5,%0nt"
  169. "movel %5,%3@+nt"
  170.      "14:t"
  171. "movesl %2@+,%5nt"
  172. "addxl %5,%0nt"
  173. "movel %5,%3@+nt"
  174.      "15:t"
  175. "movesl %2@+,%5nt"
  176. "addxl %5,%0nt"
  177. "movel %5,%3@+nt"
  178.      "16:t"
  179. "movesl %2@+,%5nt"
  180. "addxl %5,%0nt"
  181. "movel %5,%3@+nt"
  182.      "17:t"
  183. "movesl %2@+,%5nt"
  184. "addxl %5,%0nt"
  185. "movel %5,%3@+nt"
  186.      "18:t"
  187. "movesl %2@+,%5nt"
  188. "addxl %5,%0nt"
  189. "movel %5,%3@+nt"
  190. "dbra %1,1bnt"
  191. "clrl %5nt"
  192. "addxl %5,%0nt" /* add X bit */
  193. "clrw %1nt"
  194. "subql #1,%1nt"
  195. "jcc 1bn"
  196.      "2:t"
  197. "movel %4,%1nt" /* restore len from tmp1 */
  198. "andw #0x1c,%4nt" /* number of rest longs */
  199. "jeq 4fnt"
  200. "lsrw #2,%4nt"
  201. "subqw #1,%4n"
  202.      "3:n"
  203. /* loop for rest longs */
  204.      "19:t"
  205. "movesl %2@+,%5nt"
  206. "addxl %5,%0nt"
  207. "movel %5,%3@+nt"
  208. "dbra %4,3bnt"
  209. "clrl %5nt"
  210. "addxl %5,%0n" /* add X bit */
  211.      "4:t"
  212. /* now check for rest bytes that do not fit into longs */
  213. "andw #3,%1nt"
  214. "jeq 7fnt"
  215. "clrl %5nt" /* clear tmp2 for rest bytes */
  216. "subqw #2,%1nt"
  217. "jlt 5fnt"
  218.      "20:t"
  219. "movesw %2@+,%5nt" /* have rest >= 2: get word */
  220. "movew %5,%3@+nt"
  221. "swap %5nt" /* into bits 16..31 */
  222. "tstw %1nt" /* another byte? */
  223. "jeq 6fn"
  224.      "5:n"
  225.      "21:t"
  226. "movesb %2@,%5nt" /* have odd rest: get byte */
  227. "moveb %5,%3@+nt"
  228. "lslw #8,%5nt" /* into bits 8..15; 16..31 untouched */
  229.      "6:t"
  230. "addl %5,%0nt" /* now add rest long to sum */
  231. "clrl %5nt"
  232. "addxl %5,%0nt" /* add X bit */
  233.      "7:t"
  234. "clrl %5n" /* no error - clear return value */
  235.      "8:n"
  236. ".section .fixup,"ax"n"
  237. ".evenn"
  238. /* If any execption occurs zero out the rest.
  239.    Similarities with the code above are intentional :-) */
  240.      "90:t"
  241. "clrw %3@+nt"
  242. "movel %1,%4nt"
  243. "lsrl #5,%1nt"
  244. "jeq 1fnt"
  245. "subql #1,%1n"
  246.      "91:t"
  247. "clrl %3@+n"
  248.      "92:t"
  249. "clrl %3@+n"
  250.      "93:t"
  251. "clrl %3@+n"
  252.      "94:t"
  253. "clrl %3@+n"
  254.      "95:t"
  255. "clrl %3@+n"
  256.      "96:t"
  257. "clrl %3@+n"
  258.      "97:t"
  259. "clrl %3@+n"
  260.      "98:t"
  261. "clrl %3@+nt"
  262. "dbra %1,91bnt"
  263. "clrw %1nt"
  264. "subql #1,%1nt"
  265. "jcc 91bn"
  266.      "1:t"
  267. "movel %4,%1nt"
  268. "andw #0x1c,%4nt"
  269. "jeq 1fnt"
  270. "lsrw #2,%4nt"
  271. "subqw #1,%4n"
  272.      "99:t"
  273. "clrl %3@+nt"
  274. "dbra %4,99bnt"
  275.      "1:t"
  276. "andw #3,%1nt"
  277. "jeq 9fn"
  278.      "100:t"
  279. "clrw %3@+nt"
  280. "tstw %1nt"
  281. "jeq 9fn"
  282.      "101:t"
  283. "clrb %3@+n"
  284.      "9:t"
  285. #define STR(X) STR1(X)
  286. #define STR1(X) #X
  287. "moveq #-" STR(EFAULT) ",%5nt"
  288. "jra 8bn"
  289. ".previousn"
  290. ".section __ex_table,"a"n"
  291. ".long 10b,90bn"
  292. ".long 11b,91bn"
  293. ".long 12b,92bn"
  294. ".long 13b,93bn"
  295. ".long 14b,94bn"
  296. ".long 15b,95bn"
  297. ".long 16b,96bn"
  298. ".long 17b,97bn"
  299. ".long 18b,98bn"
  300. ".long 19b,99bn"
  301. ".long 20b,100bn"
  302. ".long 21b,101bn"
  303. ".previous"
  304. : "=d" (sum), "=d" (len), "=a" (src), "=a" (dst),
  305.   "=&d" (tmp1), "=d" (tmp2)
  306. : "0" (sum), "1" (len), "2" (src), "3" (dst)
  307.     );
  308. *csum_err = tmp2;
  309. return(sum);
  310. }
  311. /*
  312.  * copy from kernel space while checksumming, otherwise like csum_partial
  313.  */
  314. unsigned int
  315. csum_partial_copy(const char *src, char *dst, int len, int sum)
  316. {
  317. unsigned long tmp1, tmp2;
  318. __asm__("movel %2,%4nt"
  319. "btst #1,%4nt" /* Check alignment */
  320. "jeq 2fnt"
  321. "subql #2,%1nt" /* buff%4==2: treat first word */
  322. "jgt 1fnt"
  323. "addql #2,%1nt" /* len was == 2, treat only rest */
  324. "jra 4fn"
  325.      "1:t"
  326. "movew %2@+,%4nt" /* add first word to sum */
  327. "addw %4,%0nt"
  328. "movew %4,%3@+nt"
  329. "clrl %4nt"
  330. "addxl %4,%0n" /* add X bit */
  331.      "2:t"
  332. /* unrolled loop for the main part: do 8 longs at once */
  333. "movel %1,%4nt" /* save len in tmp1 */
  334. "lsrl #5,%1nt" /* len/32 */
  335. "jeq 2fnt" /* not enough... */
  336. "subql #1,%1n"
  337.      "1:t"
  338. "movel %2@+,%5nt"
  339. "addxl %5,%0nt"
  340. "movel %5,%3@+nt"
  341. "movel %2@+,%5nt"
  342. "addxl %5,%0nt"
  343. "movel %5,%3@+nt"
  344. "movel %2@+,%5nt"
  345. "addxl %5,%0nt"
  346. "movel %5,%3@+nt"
  347. "movel %2@+,%5nt"
  348. "addxl %5,%0nt"
  349. "movel %5,%3@+nt"
  350. "movel %2@+,%5nt"
  351. "addxl %5,%0nt"
  352. "movel %5,%3@+nt"
  353. "movel %2@+,%5nt"
  354. "addxl %5,%0nt"
  355. "movel %5,%3@+nt"
  356. "movel %2@+,%5nt"
  357. "addxl %5,%0nt"
  358. "movel %5,%3@+nt"
  359. "movel %2@+,%5nt"
  360. "addxl %5,%0nt"
  361. "movel %5,%3@+nt"
  362. "dbra %1,1bnt"
  363. "clrl %5nt"
  364. "addxl %5,%0nt" /* add X bit */
  365. "clrw %1nt"
  366. "subql #1,%1nt"
  367. "jcc 1bn"
  368.      "2:t"
  369. "movel %4,%1nt" /* restore len from tmp1 */
  370. "andw #0x1c,%4nt" /* number of rest longs */
  371. "jeq 4fnt"
  372. "lsrw #2,%4nt"
  373. "subqw #1,%4n"
  374.      "3:t"
  375. /* loop for rest longs */
  376. "movel %2@+,%5nt"
  377. "addxl %5,%0nt"
  378. "movel %5,%3@+nt"
  379. "dbra %4,3bnt"
  380. "clrl %5nt"
  381. "addxl %5,%0n" /* add X bit */
  382.      "4:t"
  383. /* now check for rest bytes that do not fit into longs */
  384. "andw #3,%1nt"
  385. "jeq 7fnt"
  386. "clrl %5nt" /* clear tmp2 for rest bytes */
  387. "subqw #2,%1nt"
  388. "jlt 5fnt"
  389. "movew %2@+,%5nt" /* have rest >= 2: get word */
  390. "movew %5,%3@+nt"
  391. "swap %5nt" /* into bits 16..31 */
  392. "tstw %1nt" /* another byte? */
  393. "jeq 6fn"
  394.      "5:t"
  395. "moveb %2@,%5nt" /* have odd rest: get byte */
  396. "moveb %5,%3@+nt"
  397. "lslw #8,%5n" /* into bits 8..15; 16..31 untouched */
  398.      "6:t"
  399. "addl %5,%0nt" /* now add rest long to sum */
  400. "clrl %5nt"
  401. "addxl %5,%0n" /* add X bit */
  402.      "7:t"
  403. : "=d" (sum), "=d" (len), "=a" (src), "=a" (dst),
  404.   "=&d" (tmp1), "=&d" (tmp2)
  405. : "0" (sum), "1" (len), "2" (src), "3" (dst)
  406.     );
  407.     return(sum);
  408. }