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.
6 * MIPS specific IP/TCP/UDP checksumming routines
8 * Authors: Ralf Baechle, <ralf@waldorf-gmbh.de>
9 * Lots of code moved from tcp.c and ip.c; see those files
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.
17 * $Id: checksum.c,v 1.1 2002/09/28 14:58:40 gerg Exp $
19 #include <net/checksum.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <asm/byteorder.h>
23 #include <asm/string.h>
24 #include <asm/uaccess.h>
26 static inline unsigned short from32to16 (unsigned long sum
)
32 add %1, %0 H L H+L+C H+L
34 asm ("hsw %1, %0; add %1, %0" : "=&r" (result
) : "r" (sum
));
38 static inline unsigned int do_csum(const unsigned char * buff
, int len
)
41 unsigned int result
= 0;
45 odd
= 1 & (unsigned long) buff
;
47 result
= be16_to_cpu(*buff
);
51 count
= len
>> 1; /* nr of 16-bit words.. */
53 if (2 & (unsigned long) buff
) {
54 result
+= *(unsigned short *) buff
;
59 count
>>= 1; /* nr of 32-bit words.. */
61 unsigned int carry
= 0;
63 unsigned int w
= *(unsigned int *) buff
;
71 result
= (result
& 0xffff) + (result
>> 16);
74 result
+= *(unsigned short *) buff
;
79 result
+= le16_to_cpu(*buff
);
80 result
= from32to16(result
);
82 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
88 * This is a version of ip_compute_csum() optimized for IP headers,
89 * which always checksum on 4 octet boundaries.
91 __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
93 return (__force __sum16
)~do_csum(iph
,ihl
*4);
97 * this routine is used for miscellaneous IP-like checksums, mainly
100 __sum16
ip_compute_csum(const void *buff
, int len
)
102 return (__force __sum16
)~do_csum(buff
,len
);
106 * computes a partial checksum, e.g. for TCP/UDP fragments
108 __wsum
csum_partial(const void *buff
, int len
, __wsum sum
)
110 unsigned int result
= do_csum(buff
, len
);
112 /* add in old sum, and carry.. */
113 result
+= (__force u32
)sum
;
114 if ((__force u32
)sum
> result
)
116 return (__force __wsum
)result
;
119 EXPORT_SYMBOL(csum_partial
);
122 * copy while checksumming, otherwise like csum_partial
124 __wsum
csum_partial_copy_nocheck(const void *src
, void *dst
,
128 * It's 2:30 am and I don't feel like doing it real ...
129 * This is lots slower than the real thing (tm)
131 sum
= csum_partial(src
, len
, sum
);
132 memcpy(dst
, src
, len
);
138 * Copy from userspace and compute checksum. If we catch an exception
139 * then zero the rest of the buffer.
141 __wsum
csum_partial_copy_from_user (const void *src
,
148 missing
= copy_from_user(dst
, src
, len
);
150 memset(dst
+ len
- missing
, 0, missing
);
154 return csum_partial(dst
, len
, sum
);