http://www.usr.com/support/gpl/USR9107_release1.1.tar.gz
[bcm963xx.git] / kernel / linux / arch / parisc / lib / checksum.c
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 static inline unsigned short from32to16(unsigned int x)
28 {
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;
34 }
35
36 static inline unsigned int do_csum(const unsigned char * buff, int len)
37 {
38         int odd, count;
39         unsigned int result = 0;
40
41         if (len <= 0)
42                 goto out;
43         odd = 1 & (unsigned long) buff;
44         if (odd) {
45                 result = be16_to_cpu(*buff);
46                 len--;
47                 buff++;
48         }
49         count = len >> 1;               /* nr of 16-bit words.. */
50         if (count) {
51                 if (2 & (unsigned long) buff) {
52                         result += *(unsigned short *) buff;
53                         count--;
54                         len -= 2;
55                         buff += 2;
56                 }
57                 count >>= 1;            /* nr of 32-bit words.. */
58                 if (count) {
59                         unsigned int carry = 0;
60                         do {
61                                 unsigned int w = *(unsigned int *) buff;
62                                 count--;
63                                 buff += 4;
64                                 result += carry;
65                                 result += w;
66                                 carry = (w > result);
67                         } while (count);
68                         result += carry;
69                         result = (result & 0xffff) + (result >> 16);
70                 }
71                 if (len & 2) {
72                         result += *(unsigned short *) buff;
73                         buff += 2;
74                 }
75         }
76         if (len & 1)
77                 result += le16_to_cpu(*buff);
78         result = from32to16(result);
79         if (odd)
80                 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
81 out:
82         return result;
83 }
84
85 /*
86  * computes a partial checksum, e.g. for TCP/UDP fragments
87  */
88 unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
89 {
90         unsigned int result = do_csum(buff, len);
91
92         /* add in old sum, and carry.. */
93         result += sum;
94         if(sum > result)
95                 result += 1;
96         return result;
97 }
98
99 EXPORT_SYMBOL(csum_partial);
100
101 /*
102  * copy while checksumming, otherwise like csum_partial
103  */
104 unsigned int csum_partial_copy_nocheck(const char *src, char *dst, 
105                                        int len, unsigned int sum)
106 {
107         /*
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)
110          */
111         sum = csum_partial(src, len, sum);
112         memcpy(dst, src, len);
113
114         return sum;
115 }
116 EXPORT_SYMBOL(csum_partial_copy_nocheck);
117
118 /*
119  * Copy from userspace and compute checksum.  If we catch an exception
120  * then zero the rest of the buffer.
121  */
122 unsigned int csum_partial_copy_from_user (const char *src, char *dst,
123                                           int len, unsigned int sum,
124                                           int *err_ptr)
125 {
126         int missing;
127
128         missing = copy_from_user(dst, src, len);
129         if (missing) {
130                 memset(dst + len - missing, 0, missing);
131                 *err_ptr = -EFAULT;
132         }
133                 
134         return csum_partial(dst, len, sum);
135 }
136 EXPORT_SYMBOL(csum_partial_copy_from_user);