diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /arch/parisc/lib/checksum.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'arch/parisc/lib/checksum.c')
-rw-r--r-- | arch/parisc/lib/checksum.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/arch/parisc/lib/checksum.c b/arch/parisc/lib/checksum.c new file mode 100644 index 000000000000..8a1e08068e7d --- /dev/null +++ b/arch/parisc/lib/checksum.c | |||
@@ -0,0 +1,148 @@ | |||
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 | * MIPS specific IP/TCP/UDP checksumming routines | ||
7 | * | ||
8 | * Authors: Ralf Baechle, <ralf@waldorf-gmbh.de> | ||
9 | * Lots of code moved from tcp.c and ip.c; see those files | ||
10 | * for more names. | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version | ||
15 | * 2 of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * $Id: checksum.c,v 1.3 1997/12/01 17:57:34 ralf Exp $ | ||
18 | */ | ||
19 | #include <linux/module.h> | ||
20 | #include <linux/types.h> | ||
21 | |||
22 | #include <net/checksum.h> | ||
23 | #include <asm/byteorder.h> | ||
24 | #include <asm/string.h> | ||
25 | #include <asm/uaccess.h> | ||
26 | |||
27 | #define addc(_t,_r) \ | ||
28 | __asm__ __volatile__ ( \ | ||
29 | " add %0, %1, %0\n" \ | ||
30 | " addc %0, %%r0, %0\n" \ | ||
31 | : "=r"(_t) \ | ||
32 | : "r"(_r), "0"(_t)); | ||
33 | |||
34 | static inline unsigned short from32to16(unsigned int x) | ||
35 | { | ||
36 | /* 32 bits --> 16 bits + carry */ | ||
37 | x = (x & 0xffff) + (x >> 16); | ||
38 | /* 16 bits + carry --> 16 bits including carry */ | ||
39 | x = (x & 0xffff) + (x >> 16); | ||
40 | return (unsigned short)x; | ||
41 | } | ||
42 | |||
43 | static inline unsigned int do_csum(const unsigned char * buff, int len) | ||
44 | { | ||
45 | int odd, count; | ||
46 | unsigned int result = 0; | ||
47 | |||
48 | if (len <= 0) | ||
49 | goto out; | ||
50 | odd = 1 & (unsigned long) buff; | ||
51 | if (odd) { | ||
52 | result = be16_to_cpu(*buff); | ||
53 | len--; | ||
54 | buff++; | ||
55 | } | ||
56 | count = len >> 1; /* nr of 16-bit words.. */ | ||
57 | if (count) { | ||
58 | if (2 & (unsigned long) buff) { | ||
59 | result += *(unsigned short *) buff; | ||
60 | count--; | ||
61 | len -= 2; | ||
62 | buff += 2; | ||
63 | } | ||
64 | count >>= 1; /* nr of 32-bit words.. */ | ||
65 | if (count) { | ||
66 | while (count >= 4) { | ||
67 | unsigned int r1, r2, r3, r4; | ||
68 | r1 = *(unsigned int *)(buff + 0); | ||
69 | r2 = *(unsigned int *)(buff + 4); | ||
70 | r3 = *(unsigned int *)(buff + 8); | ||
71 | r4 = *(unsigned int *)(buff + 12); | ||
72 | addc(result, r1); | ||
73 | addc(result, r2); | ||
74 | addc(result, r3); | ||
75 | addc(result, r4); | ||
76 | count -= 4; | ||
77 | buff += 16; | ||
78 | } | ||
79 | while (count) { | ||
80 | unsigned int w = *(unsigned int *) buff; | ||
81 | count--; | ||
82 | buff += 4; | ||
83 | addc(result, w); | ||
84 | } | ||
85 | result = (result & 0xffff) + (result >> 16); | ||
86 | } | ||
87 | if (len & 2) { | ||
88 | result += *(unsigned short *) buff; | ||
89 | buff += 2; | ||
90 | } | ||
91 | } | ||
92 | if (len & 1) | ||
93 | result += le16_to_cpu(*buff); | ||
94 | result = from32to16(result); | ||
95 | if (odd) | ||
96 | result = swab16(result); | ||
97 | out: | ||
98 | return result; | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | * computes a partial checksum, e.g. for TCP/UDP fragments | ||
103 | */ | ||
104 | unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum) | ||
105 | { | ||
106 | unsigned int result = do_csum(buff, len); | ||
107 | addc(result, sum); | ||
108 | return from32to16(result); | ||
109 | } | ||
110 | |||
111 | EXPORT_SYMBOL(csum_partial); | ||
112 | |||
113 | /* | ||
114 | * copy while checksumming, otherwise like csum_partial | ||
115 | */ | ||
116 | unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, | ||
117 | int len, unsigned int sum) | ||
118 | { | ||
119 | /* | ||
120 | * It's 2:30 am and I don't feel like doing it real ... | ||
121 | * This is lots slower than the real thing (tm) | ||
122 | */ | ||
123 | sum = csum_partial(src, len, sum); | ||
124 | memcpy(dst, src, len); | ||
125 | |||
126 | return sum; | ||
127 | } | ||
128 | EXPORT_SYMBOL(csum_partial_copy_nocheck); | ||
129 | |||
130 | /* | ||
131 | * Copy from userspace and compute checksum. If we catch an exception | ||
132 | * then zero the rest of the buffer. | ||
133 | */ | ||
134 | unsigned int csum_partial_copy_from_user(const unsigned char __user *src, | ||
135 | unsigned char *dst, int len, | ||
136 | unsigned int sum, int *err_ptr) | ||
137 | { | ||
138 | int missing; | ||
139 | |||
140 | missing = copy_from_user(dst, src, len); | ||
141 | if (missing) { | ||
142 | memset(dst + len - missing, 0, missing); | ||
143 | *err_ptr = -EFAULT; | ||
144 | } | ||
145 | |||
146 | return csum_partial(dst, len, sum); | ||
147 | } | ||
148 | EXPORT_SYMBOL(csum_partial_copy_from_user); | ||