ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / include / sysdep-i386 / checksum.h
1 /* 
2  * Licensed under the GPL
3  */
4
5 #ifndef __UM_SYSDEP_CHECKSUM_H
6 #define __UM_SYSDEP_CHECKSUM_H
7
8 #include "linux/string.h"
9
10 /*
11  * computes the checksum of a memory block at buff, length len,
12  * and adds in "sum" (32-bit)
13  *
14  * returns a 32-bit number suitable for feeding into itself
15  * or csum_tcpudp_magic
16  *
17  * this function must be called with even lengths, except
18  * for the last fragment, which may be odd
19  *
20  * it's best to have buff aligned on a 32-bit boundary
21  */
22 unsigned int csum_partial(const unsigned char * buff, int len, 
23                           unsigned int sum);
24
25 /*
26  * the same as csum_partial, but copies from src while it
27  * checksums, and handles user-space pointer exceptions correctly, when needed.
28  *
29  * here even more important to align src and dst on a 32-bit (or even
30  * better 64-bit) boundary
31  */
32
33 unsigned int csum_partial_copy_to(const char *src, char *dst, int len, 
34                                   int sum, int *err_ptr);
35 unsigned int csum_partial_copy_from(const char *src, char *dst, int len, 
36                                     int sum, int *err_ptr);
37
38 /*
39  *      Note: when you get a NULL pointer exception here this means someone
40  *      passed in an incorrect kernel address to one of these functions.
41  *
42  *      If you use these functions directly please don't forget the
43  *      verify_area().
44  */
45
46 static __inline__
47 unsigned int csum_partial_copy_nocheck(const char *src, char *dst,
48                                        int len, int sum)
49 {
50         memcpy(dst, src, len);
51         return(csum_partial(dst, len, sum));
52 }
53
54 static __inline__
55 unsigned int csum_partial_copy_from_user(const char *src, char *dst,
56                                          int len, int sum, int *err_ptr)
57 {
58         return csum_partial_copy_from(src, dst, len, sum, err_ptr);
59 }
60
61 /*
62  * These are the old (and unsafe) way of doing checksums, a warning message 
63  * will be printed if they are used and an exception occurs.
64  *
65  * these functions should go away after some time.
66  */
67
68 #define csum_partial_copy_fromuser csum_partial_copy_from_user
69 unsigned int csum_partial_copy( const char *src, char *dst, int len, int sum);
70
71 /*
72  *      This is a version of ip_compute_csum() optimized for IP headers,
73  *      which always checksum on 4 octet boundaries.
74  *
75  *      By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
76  *      Arnt Gulbrandsen.
77  */
78 static inline unsigned short ip_fast_csum(unsigned char * iph,
79                                           unsigned int ihl)
80 {
81         unsigned int sum;
82
83         __asm__ __volatile__(
84             "movl (%1), %0      ;\n"
85             "subl $4, %2        ;\n"
86             "jbe 2f             ;\n"
87             "addl 4(%1), %0     ;\n"
88             "adcl 8(%1), %0     ;\n"
89             "adcl 12(%1), %0    ;\n"
90 "1:         adcl 16(%1), %0     ;\n"
91             "lea 4(%1), %1      ;\n"
92             "decl %2            ;\n"
93             "jne 1b             ;\n"
94             "adcl $0, %0        ;\n"
95             "movl %0, %2        ;\n"
96             "shrl $16, %0       ;\n"
97             "addw %w2, %w0      ;\n"
98             "adcl $0, %0        ;\n"
99             "notl %0            ;\n"
100 "2:                             ;\n"
101         /* Since the input registers which are loaded with iph and ipl
102            are modified, we must also specify them as outputs, or gcc
103            will assume they contain their original values. */
104         : "=r" (sum), "=r" (iph), "=r" (ihl)
105         : "1" (iph), "2" (ihl));
106         return(sum);
107 }
108
109 /*
110  *      Fold a partial checksum
111  */
112
113 static inline unsigned int csum_fold(unsigned int sum)
114 {
115         __asm__(
116                 "addl %1, %0            ;\n"
117                 "adcl $0xffff, %0       ;\n"
118                 : "=r" (sum)
119                 : "r" (sum << 16), "0" (sum & 0xffff0000)
120         );
121         return (~sum) >> 16;
122 }
123
124 static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
125                                                    unsigned long daddr,
126                                                    unsigned short len,
127                                                    unsigned short proto,
128                                                    unsigned int sum)
129 {
130     __asm__(
131         "addl %1, %0    ;\n"
132         "adcl %2, %0    ;\n"
133         "adcl %3, %0    ;\n"
134         "adcl $0, %0    ;\n"
135         : "=r" (sum)
136         : "g" (daddr), "g"(saddr), "g"((ntohs(len)<<16)+proto*256), "0"(sum));
137     return sum;
138 }
139
140 /*
141  * computes the checksum of the TCP/UDP pseudo-header
142  * returns a 16-bit checksum, already complemented
143  */
144 static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
145                                                    unsigned long daddr,
146                                                    unsigned short len,
147                                                    unsigned short proto,
148                                                    unsigned int sum)
149 {
150         return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
151 }
152
153 /*
154  * this routine is used for miscellaneous IP-like checksums, mainly
155  * in icmp.c
156  */
157
158 static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
159 {
160     return csum_fold (csum_partial(buff, len, 0));
161 }
162
163 #define _HAVE_ARCH_IPV6_CSUM
164 static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
165                                                      struct in6_addr *daddr,
166                                                      __u32 len,
167                                                      unsigned short proto,
168                                                      unsigned int sum)
169 {
170         __asm__(
171                 "addl 0(%1), %0         ;\n"
172                 "adcl 4(%1), %0         ;\n"
173                 "adcl 8(%1), %0         ;\n"
174                 "adcl 12(%1), %0        ;\n"
175                 "adcl 0(%2), %0         ;\n"
176                 "adcl 4(%2), %0         ;\n"
177                 "adcl 8(%2), %0         ;\n"
178                 "adcl 12(%2), %0        ;\n"
179                 "adcl %3, %0            ;\n"
180                 "adcl %4, %0            ;\n"
181                 "adcl $0, %0            ;\n"
182                 : "=&r" (sum)
183                 : "r" (saddr), "r" (daddr),
184                   "r"(htonl(len)), "r"(htonl(proto)), "0"(sum));
185
186         return csum_fold(sum);
187 }
188
189 /*
190  *      Copy and checksum to user
191  */
192 #define HAVE_CSUM_COPY_USER
193 static __inline__ unsigned int csum_and_copy_to_user(const char *src, 
194                                                      char *dst, int len,
195                                                      int sum, int *err_ptr)
196 {
197         if (access_ok(VERIFY_WRITE, dst, len))
198                 return(csum_partial_copy_to(src, dst, len, sum, err_ptr));
199
200         if (len)
201                 *err_ptr = -EFAULT;
202
203         return -1; /* invalid checksum */
204 }
205
206 #endif
207
208 /*
209  * Overrides for Emacs so that we follow Linus's tabbing style.
210  * Emacs will notice this stuff at the end of the file and automatically
211  * adjust the settings for this buffer only.  This must remain at the end
212  * of the file.
213  * ---------------------------------------------------------------------------
214  * Local variables:
215  * c-file-style: "linux"
216  * End:
217  */