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.3 1997/12/01 17:57:34 ralf Exp $
19 #include <linux/module.h>
20 #include <linux/types.h>
22 #include <net/checksum.h>
23 #include <asm/byteorder.h>
24 #include <asm/string.h>
25 #include <asm/uaccess.h>
27 static inline unsigned short from32to16(unsigned int x)
29 /* 32 bits --> 16 bits + carry */
30 x = (x & 0xffff) + (x >> 16);
31 /* 16 bits + carry --> 16 bits including carry */
32 x = (x & 0xffff) + (x >> 16);
33 return (unsigned short)x;
36 static inline unsigned int do_csum(const unsigned char * buff, int len)
39 unsigned int result = 0;
43 odd = 1 & (unsigned long) buff;
45 result = be16_to_cpu(*buff);
49 count = len >> 1; /* nr of 16-bit words.. */
51 if (2 & (unsigned long) buff) {
52 result += *(unsigned short *) buff;
57 count >>= 1; /* nr of 32-bit words.. */
59 unsigned int carry = 0;
61 unsigned int w = *(unsigned int *) buff;
69 result = (result & 0xffff) + (result >> 16);
72 result += *(unsigned short *) buff;
77 result += le16_to_cpu(*buff);
78 result = from32to16(result);
80 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
86 * computes a partial checksum, e.g. for TCP/UDP fragments
88 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
90 unsigned int result = do_csum(buff, len);
92 /* add in old sum, and carry.. */
99 EXPORT_SYMBOL(csum_partial);
102 * copy while checksumming, otherwise like csum_partial
104 unsigned int csum_partial_copy_nocheck(const char *src, char *dst,
105 int len, unsigned int sum)
108 * It's 2:30 am and I don't feel like doing it real ...
109 * This is lots slower than the real thing (tm)
111 sum = csum_partial(src, len, sum);
112 memcpy(dst, src, len);
116 EXPORT_SYMBOL(csum_partial_copy_nocheck);
119 * Copy from userspace and compute checksum. If we catch an exception
120 * then zero the rest of the buffer.
122 unsigned int csum_partial_copy_from_user (const char *src, char *dst,
123 int len, unsigned int sum,
128 missing = copy_from_user(dst, src, len);
130 memset(dst + len - missing, 0, missing);
134 return csum_partial(dst, len, sum);
136 EXPORT_SYMBOL(csum_partial_copy_from_user);