• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * arch/sh/lib64/c-checksum.c
3  *
4  * This file contains network checksum routines that are better done
5  * in an architecture-specific manner due to speed..
6  */
7 #include <linux/string.h>
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <asm/byteorder.h>
12 #include <asm/uaccess.h>
13 
from64to16(unsigned long long x)14 static inline unsigned short from64to16(unsigned long long x)
15 {
16 	/* add up 32-bit words for 33 bits */
17 	x = (x & 0xffffffff) + (x >> 32);
18 	/* add up 16-bit and 17-bit words for 17+c bits */
19 	x = (x & 0xffff) + (x >> 16);
20 	/* add up 16-bit and 2-bit for 16+c bit */
21 	x = (x & 0xffff) + (x >> 16);
22 	/* add up carry.. */
23 	x = (x & 0xffff) + (x >> 16);
24 	return x;
25 }
26 
foldto16(unsigned long x)27 static inline unsigned short foldto16(unsigned long x)
28 {
29 	/* add up 16-bit for 17 bits */
30 	x = (x & 0xffff) + (x >> 16);
31 	/* add up carry.. */
32 	x = (x & 0xffff) + (x >> 16);
33 	return x;
34 }
35 
myfoldto16(unsigned long long x)36 static inline unsigned short myfoldto16(unsigned long long x)
37 {
38 	/* Fold down to 32-bits so we don't lose in the typedef-less
39 	   network stack.  */
40 	/* 64 to 33 */
41 	x = (x & 0xffffffff) + (x >> 32);
42 	/* 33 to 32 */
43 	x = (x & 0xffffffff) + (x >> 32);
44 
45 	/* add up 16-bit for 17 bits */
46 	x = (x & 0xffff) + (x >> 16);
47 	/* add up carry.. */
48 	x = (x & 0xffff) + (x >> 16);
49 	return x;
50 }
51 
52 #define odd(x) ((x)&1)
53 #define U16(x) ntohs(x)
54 
do_csum(const unsigned char * buff,int len)55 static unsigned long do_csum(const unsigned char *buff, int len)
56 {
57 	int odd, count;
58 	unsigned long result = 0;
59 
60 	pr_debug("do_csum buff %p, len %d (0x%x)\n", buff, len, len);
61 #ifdef DEBUG
62 	for (i = 0; i < len; i++) {
63 		if ((i % 26) == 0)
64 			printk("\n");
65 		printk("%02X ", buff[i]);
66 	}
67 #endif
68 
69 	if (len <= 0)
70 		goto out;
71 
72 	odd = 1 & (unsigned long) buff;
73 	if (odd) {
74 		result = *buff << 8;
75 		len--;
76 		buff++;
77 	}
78 	count = len >> 1;	/* nr of 16-bit words.. */
79 	if (count) {
80 		if (2 & (unsigned long) buff) {
81 			result += *(unsigned short *) buff;
82 			count--;
83 			len -= 2;
84 			buff += 2;
85 		}
86 		count >>= 1;	/* nr of 32-bit words.. */
87 		if (count) {
88 			unsigned long carry = 0;
89 			do {
90 				unsigned long w = *(unsigned long *) buff;
91 				buff += 4;
92 				count--;
93 				result += carry;
94 				result += w;
95 				carry = (w > result);
96 			} while (count);
97 			result += carry;
98 			result = (result & 0xffff) + (result >> 16);
99 		}
100 		if (len & 2) {
101 			result += *(unsigned short *) buff;
102 			buff += 2;
103 		}
104 	}
105 	if (len & 1)
106 		result += *buff;
107 	result = foldto16(result);
108 	if (odd)
109 		result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
110 
111 	pr_debug("\nCHECKSUM is 0x%lx\n", result);
112 
113       out:
114 	return result;
115 }
116 
117 /* computes the checksum of a memory block at buff, length len,
118    and adds in "sum" (32-bit)  */
csum_partial(const void * buff,int len,__wsum sum)119 __wsum csum_partial(const void *buff, int len, __wsum sum)
120 {
121 	unsigned long long result = do_csum(buff, len);
122 
123 	/* add in old sum, and carry.. */
124 	result += (__force u32)sum;
125 	/* 32+c bits -> 32 bits */
126 	result = (result & 0xffffffff) + (result >> 32);
127 
128 	pr_debug("csum_partial, buff %p len %d sum 0x%x result=0x%016Lx\n",
129 		buff, len, sum, result);
130 
131 	return (__force __wsum)result;
132 }
133 
134 /* Copy while checksumming, otherwise like csum_partial.  */
135 __wsum
csum_partial_copy_nocheck(const void * src,void * dst,int len,__wsum sum)136 csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
137 {
138 	sum = csum_partial(src, len, sum);
139 	memcpy(dst, src, len);
140 
141 	return sum;
142 }
143 
144 /* Copy from userspace and compute checksum.  If we catch an exception
145    then zero the rest of the buffer.  */
146 __wsum
csum_partial_copy_from_user(const void __user * src,void * dst,int len,__wsum sum,int * err_ptr)147 csum_partial_copy_from_user(const void __user *src, void *dst, int len,
148 			    __wsum sum, int *err_ptr)
149 {
150 	int missing;
151 
152 	pr_debug
153 	    ("csum_partial_copy_from_user src %p, dest %p, len %d, sum %08x, err_ptr %p\n",
154 	     src, dst, len, sum, err_ptr);
155 	missing = copy_from_user(dst, src, len);
156 	pr_debug("  access_ok %d\n", __access_ok((unsigned long) src, len));
157 	pr_debug("  missing %d\n", missing);
158 	if (missing) {
159 		memset(dst + len - missing, 0, missing);
160 		*err_ptr = -EFAULT;
161 	}
162 
163 	return csum_partial(dst, len, sum);
164 }
165 
166 /* Copy to userspace and compute checksum.  */
167 __wsum
csum_partial_copy_to_user(const unsigned char * src,unsigned char * dst,int len,__wsum sum,int * err_ptr)168 csum_partial_copy_to_user(const unsigned char *src, unsigned char *dst, int len,
169 			  __wsum sum, int *err_ptr)
170 {
171 	sum = csum_partial(src, len, sum);
172 
173 	if (copy_to_user(dst, src, len))
174 		*err_ptr = -EFAULT;
175 
176 	return sum;
177 }
178 
179 /*
180  *	This is a version of ip_compute_csum() optimized for IP headers,
181  *	which always checksum on 4 octet boundaries.
182  */
ip_fast_csum(const void * iph,unsigned int ihl)183 __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
184 {
185 	pr_debug("ip_fast_csum %p,%d\n", iph, ihl);
186 
187 	return (__force __sum16)~do_csum(iph, ihl * 4);
188 }
189 
csum_tcpudp_nofold(__be32 saddr,__be32 daddr,unsigned short len,unsigned short proto,__wsum sum)190 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
191 				unsigned short len,
192 				unsigned short proto, __wsum sum)
193 {
194 	unsigned long long result;
195 
196 	pr_debug("ntohs(0x%x)=0x%x\n", 0xdead, ntohs(0xdead));
197 	pr_debug("htons(0x%x)=0x%x\n", 0xdead, htons(0xdead));
198 
199 	result = (__force u64) saddr + (__force u64) daddr +
200 		 (__force u64) sum + ((len + proto) << 8);
201 
202 	/* Fold down to 32-bits so we don't lose in the typedef-less
203 	   network stack.  */
204 	/* 64 to 33 */
205 	result = (result & 0xffffffff) + (result >> 32);
206 	/* 33 to 32 */
207 	result = (result & 0xffffffff) + (result >> 32);
208 
209 	pr_debug("%s saddr %x daddr %x len %x proto %x sum %x result %08Lx\n",
210 		__func__, saddr, daddr, len, proto, sum, result);
211 
212 	return (__wsum)result;
213 }
214 EXPORT_SYMBOL(csum_tcpudp_nofold);
215