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