My fork of the HTTPServer (working)
Embed:
(wiki syntax)
Show/hide line numbers
inet_chksum.c
Go to the documentation of this file.
00001 /** 00002 * @file 00003 * Incluse internet checksum functions. 00004 * 00005 */ 00006 00007 /* 00008 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00009 * All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without modification, 00012 * are permitted provided that the following conditions are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00023 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00025 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00026 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00027 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00030 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00031 * OF SUCH DAMAGE. 00032 * 00033 * This file is part of the lwIP TCP/IP stack. 00034 * 00035 * Author: Adam Dunkels <adam@sics.se> 00036 * 00037 */ 00038 00039 #include "lwip/opt.h" 00040 00041 #include "lwip/inet_chksum.h" 00042 #include "lwip/inet.h" 00043 00044 /* These are some reference implementations of the checksum algorithm, with the 00045 * aim of being simple, correct and fully portable. Checksumming is the 00046 * first thing you would want to optimize for your platform. If you create 00047 * your own version, link it in and in your cc.h put: 00048 * 00049 * #define LWIP_CHKSUM <your_checksum_routine> 00050 * 00051 * Or you can select from the implementations below by defining 00052 * LWIP_CHKSUM_ALGORITHM to 1, 2 or 3. 00053 */ 00054 00055 #ifndef LWIP_CHKSUM 00056 # define LWIP_CHKSUM lwip_standard_chksum 00057 # ifndef LWIP_CHKSUM_ALGORITHM 00058 # define LWIP_CHKSUM_ALGORITHM 1 00059 # endif 00060 #endif 00061 /* If none set: */ 00062 #ifndef LWIP_CHKSUM_ALGORITHM 00063 # define LWIP_CHKSUM_ALGORITHM 0 00064 #endif 00065 00066 /** Like the name says... */ 00067 #define SWAP_BYTES_IN_WORD(w) ((w & 0xff) << 8) | ((w & 0xff00) >> 8) 00068 /** Split an u32_t in two u16_ts and add them up */ 00069 #define FOLD_U32T(u) ((u >> 16) + (u & 0x0000ffffUL)) 00070 00071 #if (LWIP_CHKSUM_ALGORITHM == 1) /* Version #1 */ 00072 /** 00073 * lwip checksum 00074 * 00075 * @param dataptr points to start of data to be summed at any boundary 00076 * @param len length of data to be summed 00077 * @return host order (!) lwip checksum (non-inverted Internet sum) 00078 * 00079 * @note accumulator size limits summable length to 64k 00080 * @note host endianess is irrelevant (p3 RFC1071) 00081 */ 00082 static u16_t 00083 lwip_standard_chksum(void *dataptr, u16_t len) 00084 { 00085 u32_t acc; 00086 u16_t src; 00087 u8_t *octetptr; 00088 00089 acc = 0; 00090 /* dataptr may be at odd or even addresses */ 00091 octetptr = (u8_t*)dataptr; 00092 while (len > 1) { 00093 /* declare first octet as most significant 00094 thus assume network order, ignoring host order */ 00095 src = (*octetptr) << 8; 00096 octetptr++; 00097 /* declare second octet as least significant */ 00098 src |= (*octetptr); 00099 octetptr++; 00100 acc += src; 00101 len -= 2; 00102 } 00103 if (len > 0) { 00104 /* accumulate remaining octet */ 00105 src = (*octetptr) << 8; 00106 acc += src; 00107 } 00108 /* add deferred carry bits */ 00109 acc = (acc >> 16) + (acc & 0x0000ffffUL); 00110 if ((acc & 0xffff0000UL) != 0) { 00111 acc = (acc >> 16) + (acc & 0x0000ffffUL); 00112 } 00113 /* This maybe a little confusing: reorder sum using htons() 00114 instead of ntohs() since it has a little less call overhead. 00115 The caller must invert bits for Internet sum ! */ 00116 return htons((u16_t)acc); 00117 } 00118 #endif 00119 00120 #if (LWIP_CHKSUM_ALGORITHM == 2) /* Alternative version #2 */ 00121 /* 00122 * Curt McDowell 00123 * Broadcom Corp. 00124 * csm@broadcom.com 00125 * 00126 * IP checksum two bytes at a time with support for 00127 * unaligned buffer. 00128 * Works for len up to and including 0x20000. 00129 * by Curt McDowell, Broadcom Corp. 12/08/2005 00130 * 00131 * @param dataptr points to start of data to be summed at any boundary 00132 * @param len length of data to be summed 00133 * @return host order (!) lwip checksum (non-inverted Internet sum) 00134 */ 00135 00136 static u16_t 00137 lwip_standard_chksum(void *dataptr, int len) 00138 { 00139 u8_t *pb = dataptr; 00140 u16_t *ps, t = 0; 00141 u32_t sum = 0; 00142 int odd = ((u32_t)pb & 1); 00143 00144 /* Get aligned to u16_t */ 00145 if (odd && len > 0) { 00146 ((u8_t *)&t)[1] = *pb++; 00147 len--; 00148 } 00149 00150 /* Add the bulk of the data */ 00151 ps = (u16_t *)pb; 00152 while (len > 1) { 00153 sum += *ps++; 00154 len -= 2; 00155 } 00156 00157 /* Consume left-over byte, if any */ 00158 if (len > 0) { 00159 ((u8_t *)&t)[0] = *(u8_t *)ps;; 00160 } 00161 00162 /* Add end bytes */ 00163 sum += t; 00164 00165 /* Fold 32-bit sum to 16 bits 00166 calling this twice is propably faster than if statements... */ 00167 sum = FOLD_U32T(sum); 00168 sum = FOLD_U32T(sum); 00169 00170 /* Swap if alignment was odd */ 00171 if (odd) { 00172 sum = SWAP_BYTES_IN_WORD(sum); 00173 } 00174 00175 return sum; 00176 } 00177 #endif 00178 00179 #if (LWIP_CHKSUM_ALGORITHM == 3) /* Alternative version #3 */ 00180 /** 00181 * An optimized checksum routine. Basically, it uses loop-unrolling on 00182 * the checksum loop, treating the head and tail bytes specially, whereas 00183 * the inner loop acts on 8 bytes at a time. 00184 * 00185 * @arg start of buffer to be checksummed. May be an odd byte address. 00186 * @len number of bytes in the buffer to be checksummed. 00187 * @return host order (!) lwip checksum (non-inverted Internet sum) 00188 * 00189 * by Curt McDowell, Broadcom Corp. December 8th, 2005 00190 */ 00191 00192 static u16_t 00193 lwip_standard_chksum(void *dataptr, int len) 00194 { 00195 u8_t *pb = dataptr; 00196 u16_t *ps, t = 0; 00197 u32_t *pl; 00198 u32_t sum = 0, tmp; 00199 /* starts at odd byte address? */ 00200 int odd = ((u32_t)pb & 1); 00201 00202 if (odd && len > 0) { 00203 ((u8_t *)&t)[1] = *pb++; 00204 len--; 00205 } 00206 00207 ps = (u16_t *)pb; 00208 00209 if (((u32_t)ps & 3) && len > 1) { 00210 sum += *ps++; 00211 len -= 2; 00212 } 00213 00214 pl = (u32_t *)ps; 00215 00216 while (len > 7) { 00217 tmp = sum + *pl++; /* ping */ 00218 if (tmp < sum) { 00219 tmp++; /* add back carry */ 00220 } 00221 00222 sum = tmp + *pl++; /* pong */ 00223 if (sum < tmp) { 00224 sum++; /* add back carry */ 00225 } 00226 00227 len -= 8; 00228 } 00229 00230 /* make room in upper bits */ 00231 sum = FOLD_U32T(sum); 00232 00233 ps = (u16_t *)pl; 00234 00235 /* 16-bit aligned word remaining? */ 00236 while (len > 1) { 00237 sum += *ps++; 00238 len -= 2; 00239 } 00240 00241 /* dangling tail byte remaining? */ 00242 if (len > 0) { /* include odd byte */ 00243 ((u8_t *)&t)[0] = *(u8_t *)ps; 00244 } 00245 00246 sum += t; /* add end bytes */ 00247 00248 /* Fold 32-bit sum to 16 bits 00249 calling this twice is propably faster than if statements... */ 00250 sum = FOLD_U32T(sum); 00251 sum = FOLD_U32T(sum); 00252 00253 if (odd) { 00254 sum = SWAP_BYTES_IN_WORD(sum); 00255 } 00256 00257 return sum; 00258 } 00259 #endif 00260 00261 /* inet_chksum_pseudo: 00262 * 00263 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain. 00264 * IP addresses are expected to be in network byte order. 00265 * 00266 * @param p chain of pbufs over that a checksum should be calculated (ip data part) 00267 * @param src source ip address (used for checksum of pseudo header) 00268 * @param dst destination ip address (used for checksum of pseudo header) 00269 * @param proto ip protocol (used for checksum of pseudo header) 00270 * @param proto_len length of the ip data part (used for checksum of pseudo header) 00271 * @return checksum (as u16_t) to be saved directly in the protocol header 00272 */ 00273 u16_t 00274 inet_chksum_pseudo(struct pbuf *p, 00275 struct ip_addr *src, struct ip_addr *dest, 00276 u8_t proto, u16_t proto_len) 00277 { 00278 u32_t acc; 00279 struct pbuf *q; 00280 u8_t swapped; 00281 00282 acc = 0; 00283 swapped = 0; 00284 /* iterate through all pbuf in chain */ 00285 for(q = p; q != NULL; q = q->next) { 00286 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n", 00287 (void *)q, (void *)q->next)); 00288 acc += LWIP_CHKSUM(q->payload, q->len); 00289 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/ 00290 /* just executing this next line is probably faster that the if statement needed 00291 to check whether we really need to execute it, and does no harm */ 00292 acc = FOLD_U32T(acc); 00293 if (q->len % 2 != 0) { 00294 swapped = 1 - swapped; 00295 acc = SWAP_BYTES_IN_WORD(acc); 00296 } 00297 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/ 00298 } 00299 00300 if (swapped) { 00301 acc = SWAP_BYTES_IN_WORD(acc); 00302 } 00303 acc += (src->addr & 0xffffUL); 00304 acc += ((src->addr >> 16) & 0xffffUL); 00305 acc += (dest->addr & 0xffffUL); 00306 acc += ((dest->addr >> 16) & 0xffffUL); 00307 acc += (u32_t)htons((u16_t)proto); 00308 acc += (u32_t)htons(proto_len); 00309 00310 /* Fold 32-bit sum to 16 bits 00311 calling this twice is propably faster than if statements... */ 00312 acc = FOLD_U32T(acc); 00313 acc = FOLD_U32T(acc); 00314 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc)); 00315 return (u16_t)~(acc & 0xffffUL); 00316 } 00317 00318 /* inet_chksum_pseudo: 00319 * 00320 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain. 00321 * IP addresses are expected to be in network byte order. 00322 * 00323 * @param p chain of pbufs over that a checksum should be calculated (ip data part) 00324 * @param src source ip address (used for checksum of pseudo header) 00325 * @param dst destination ip address (used for checksum of pseudo header) 00326 * @param proto ip protocol (used for checksum of pseudo header) 00327 * @param proto_len length of the ip data part (used for checksum of pseudo header) 00328 * @return checksum (as u16_t) to be saved directly in the protocol header 00329 */ 00330 /* Currently only used by UDPLITE, although this could change in the future. */ 00331 #if LWIP_UDPLITE 00332 u16_t 00333 inet_chksum_pseudo_partial(struct pbuf *p, 00334 struct ip_addr *src, struct ip_addr *dest, 00335 u8_t proto, u16_t proto_len, u16_t chksum_len) 00336 { 00337 u32_t acc; 00338 struct pbuf *q; 00339 u8_t swapped; 00340 u16_t chklen; 00341 00342 acc = 0; 00343 swapped = 0; 00344 /* iterate through all pbuf in chain */ 00345 for(q = p; (q != NULL) && (chksum_len > 0); q = q->next) { 00346 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n", 00347 (void *)q, (void *)q->next)); 00348 chklen = q->len; 00349 if (chklen > chksum_len) { 00350 chklen = chksum_len; 00351 } 00352 acc += LWIP_CHKSUM(q->payload, chklen); 00353 chksum_len -= chklen; 00354 LWIP_ASSERT("delete me", chksum_len < 0x7fff); 00355 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/ 00356 /* fold the upper bit down */ 00357 acc = FOLD_U32T(acc); 00358 if (q->len % 2 != 0) { 00359 swapped = 1 - swapped; 00360 acc = SWAP_BYTES_IN_WORD(acc); 00361 } 00362 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/ 00363 } 00364 00365 if (swapped) { 00366 acc = SWAP_BYTES_IN_WORD(acc); 00367 } 00368 acc += (src->addr & 0xffffUL); 00369 acc += ((src->addr >> 16) & 0xffffUL); 00370 acc += (dest->addr & 0xffffUL); 00371 acc += ((dest->addr >> 16) & 0xffffUL); 00372 acc += (u32_t)htons((u16_t)proto); 00373 acc += (u32_t)htons(proto_len); 00374 00375 /* Fold 32-bit sum to 16 bits 00376 calling this twice is propably faster than if statements... */ 00377 acc = FOLD_U32T(acc); 00378 acc = FOLD_U32T(acc); 00379 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc)); 00380 return (u16_t)~(acc & 0xffffUL); 00381 } 00382 #endif /* LWIP_UDPLITE */ 00383 00384 /* inet_chksum: 00385 * 00386 * Calculates the Internet checksum over a portion of memory. Used primarily for IP 00387 * and ICMP. 00388 * 00389 * @param dataptr start of the buffer to calculate the checksum (no alignment needed) 00390 * @param len length of the buffer to calculate the checksum 00391 * @return checksum (as u16_t) to be saved directly in the protocol header 00392 */ 00393 00394 u16_t 00395 inet_chksum(void *dataptr, u16_t len) 00396 { 00397 return ~LWIP_CHKSUM(dataptr, len); 00398 } 00399 00400 /** 00401 * Calculate a checksum over a chain of pbufs (without pseudo-header, much like 00402 * inet_chksum only pbufs are used). 00403 * 00404 * @param p pbuf chain over that the checksum should be calculated 00405 * @return checksum (as u16_t) to be saved directly in the protocol header 00406 */ 00407 u16_t 00408 inet_chksum_pbuf(struct pbuf *p) 00409 { 00410 u32_t acc; 00411 struct pbuf *q; 00412 u8_t swapped; 00413 00414 acc = 0; 00415 swapped = 0; 00416 for(q = p; q != NULL; q = q->next) { 00417 acc += LWIP_CHKSUM(q->payload, q->len); 00418 acc = FOLD_U32T(acc); 00419 if (q->len % 2 != 0) { 00420 swapped = 1 - swapped; 00421 acc = SWAP_BYTES_IN_WORD(acc); 00422 } 00423 } 00424 00425 if (swapped) { 00426 acc = SWAP_BYTES_IN_WORD(acc); 00427 } 00428 return (u16_t)~(acc & 0xffffUL); 00429 }
Generated on Tue Jul 12 2022 19:24:05 by 1.7.2