Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
segundo 0:ac1725ba162c 1 /**
segundo 0:ac1725ba162c 2 * @file
segundo 0:ac1725ba162c 3 * Incluse internet checksum functions.
segundo 0:ac1725ba162c 4 *
segundo 0:ac1725ba162c 5 */
segundo 0:ac1725ba162c 6
segundo 0:ac1725ba162c 7 /*
segundo 0:ac1725ba162c 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
segundo 0:ac1725ba162c 9 * All rights reserved.
segundo 0:ac1725ba162c 10 *
segundo 0:ac1725ba162c 11 * Redistribution and use in source and binary forms, with or without modification,
segundo 0:ac1725ba162c 12 * are permitted provided that the following conditions are met:
segundo 0:ac1725ba162c 13 *
segundo 0:ac1725ba162c 14 * 1. Redistributions of source code must retain the above copyright notice,
segundo 0:ac1725ba162c 15 * this list of conditions and the following disclaimer.
segundo 0:ac1725ba162c 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
segundo 0:ac1725ba162c 17 * this list of conditions and the following disclaimer in the documentation
segundo 0:ac1725ba162c 18 * and/or other materials provided with the distribution.
segundo 0:ac1725ba162c 19 * 3. The name of the author may not be used to endorse or promote products
segundo 0:ac1725ba162c 20 * derived from this software without specific prior written permission.
segundo 0:ac1725ba162c 21 *
segundo 0:ac1725ba162c 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
segundo 0:ac1725ba162c 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
segundo 0:ac1725ba162c 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
segundo 0:ac1725ba162c 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
segundo 0:ac1725ba162c 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
segundo 0:ac1725ba162c 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
segundo 0:ac1725ba162c 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
segundo 0:ac1725ba162c 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
segundo 0:ac1725ba162c 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
segundo 0:ac1725ba162c 31 * OF SUCH DAMAGE.
segundo 0:ac1725ba162c 32 *
segundo 0:ac1725ba162c 33 * This file is part of the lwIP TCP/IP stack.
segundo 0:ac1725ba162c 34 *
segundo 0:ac1725ba162c 35 * Author: Adam Dunkels <adam@sics.se>
segundo 0:ac1725ba162c 36 *
segundo 0:ac1725ba162c 37 */
segundo 0:ac1725ba162c 38
segundo 0:ac1725ba162c 39 #include "lwip/opt.h"
segundo 0:ac1725ba162c 40
segundo 0:ac1725ba162c 41 #include "lwip/inet_chksum.h"
segundo 0:ac1725ba162c 42 #include "lwip/def.h"
segundo 0:ac1725ba162c 43
segundo 0:ac1725ba162c 44 #include <stddef.h>
segundo 0:ac1725ba162c 45 #include <string.h>
segundo 0:ac1725ba162c 46
segundo 0:ac1725ba162c 47 /* These are some reference implementations of the checksum algorithm, with the
segundo 0:ac1725ba162c 48 * aim of being simple, correct and fully portable. Checksumming is the
segundo 0:ac1725ba162c 49 * first thing you would want to optimize for your platform. If you create
segundo 0:ac1725ba162c 50 * your own version, link it in and in your cc.h put:
segundo 0:ac1725ba162c 51 *
segundo 0:ac1725ba162c 52 * #define LWIP_CHKSUM <your_checksum_routine>
segundo 0:ac1725ba162c 53 *
segundo 0:ac1725ba162c 54 * Or you can select from the implementations below by defining
segundo 0:ac1725ba162c 55 * LWIP_CHKSUM_ALGORITHM to 1, 2 or 3.
segundo 0:ac1725ba162c 56 */
segundo 0:ac1725ba162c 57
segundo 0:ac1725ba162c 58 #ifndef LWIP_CHKSUM
segundo 0:ac1725ba162c 59 # define LWIP_CHKSUM lwip_standard_chksum
segundo 0:ac1725ba162c 60 # ifndef LWIP_CHKSUM_ALGORITHM
segundo 0:ac1725ba162c 61 # define LWIP_CHKSUM_ALGORITHM 2
segundo 0:ac1725ba162c 62 # endif
segundo 0:ac1725ba162c 63 #endif
segundo 0:ac1725ba162c 64 /* If none set: */
segundo 0:ac1725ba162c 65 #ifndef LWIP_CHKSUM_ALGORITHM
segundo 0:ac1725ba162c 66 # define LWIP_CHKSUM_ALGORITHM 0
segundo 0:ac1725ba162c 67 #endif
segundo 0:ac1725ba162c 68
segundo 0:ac1725ba162c 69 #if (LWIP_CHKSUM_ALGORITHM == 1) /* Version #1 */
segundo 0:ac1725ba162c 70 /**
segundo 0:ac1725ba162c 71 * lwip checksum
segundo 0:ac1725ba162c 72 *
segundo 0:ac1725ba162c 73 * @param dataptr points to start of data to be summed at any boundary
segundo 0:ac1725ba162c 74 * @param len length of data to be summed
segundo 0:ac1725ba162c 75 * @return host order (!) lwip checksum (non-inverted Internet sum)
segundo 0:ac1725ba162c 76 *
segundo 0:ac1725ba162c 77 * @note accumulator size limits summable length to 64k
segundo 0:ac1725ba162c 78 * @note host endianess is irrelevant (p3 RFC1071)
segundo 0:ac1725ba162c 79 */
segundo 0:ac1725ba162c 80 static u16_t
segundo 0:ac1725ba162c 81 lwip_standard_chksum(void *dataptr, u16_t len)
segundo 0:ac1725ba162c 82 {
segundo 0:ac1725ba162c 83 u32_t acc;
segundo 0:ac1725ba162c 84 u16_t src;
segundo 0:ac1725ba162c 85 u8_t *octetptr;
segundo 0:ac1725ba162c 86
segundo 0:ac1725ba162c 87 acc = 0;
segundo 0:ac1725ba162c 88 /* dataptr may be at odd or even addresses */
segundo 0:ac1725ba162c 89 octetptr = (u8_t*)dataptr;
segundo 0:ac1725ba162c 90 while (len > 1) {
segundo 0:ac1725ba162c 91 /* declare first octet as most significant
segundo 0:ac1725ba162c 92 thus assume network order, ignoring host order */
segundo 0:ac1725ba162c 93 src = (*octetptr) << 8;
segundo 0:ac1725ba162c 94 octetptr++;
segundo 0:ac1725ba162c 95 /* declare second octet as least significant */
segundo 0:ac1725ba162c 96 src |= (*octetptr);
segundo 0:ac1725ba162c 97 octetptr++;
segundo 0:ac1725ba162c 98 acc += src;
segundo 0:ac1725ba162c 99 len -= 2;
segundo 0:ac1725ba162c 100 }
segundo 0:ac1725ba162c 101 if (len > 0) {
segundo 0:ac1725ba162c 102 /* accumulate remaining octet */
segundo 0:ac1725ba162c 103 src = (*octetptr) << 8;
segundo 0:ac1725ba162c 104 acc += src;
segundo 0:ac1725ba162c 105 }
segundo 0:ac1725ba162c 106 /* add deferred carry bits */
segundo 0:ac1725ba162c 107 acc = (acc >> 16) + (acc & 0x0000ffffUL);
segundo 0:ac1725ba162c 108 if ((acc & 0xffff0000UL) != 0) {
segundo 0:ac1725ba162c 109 acc = (acc >> 16) + (acc & 0x0000ffffUL);
segundo 0:ac1725ba162c 110 }
segundo 0:ac1725ba162c 111 /* This maybe a little confusing: reorder sum using htons()
segundo 0:ac1725ba162c 112 instead of ntohs() since it has a little less call overhead.
segundo 0:ac1725ba162c 113 The caller must invert bits for Internet sum ! */
segundo 0:ac1725ba162c 114 return htons((u16_t)acc);
segundo 0:ac1725ba162c 115 }
segundo 0:ac1725ba162c 116 #endif
segundo 0:ac1725ba162c 117
segundo 0:ac1725ba162c 118 #if (LWIP_CHKSUM_ALGORITHM == 2) /* Alternative version #2 */
segundo 0:ac1725ba162c 119 /*
segundo 0:ac1725ba162c 120 * Curt McDowell
segundo 0:ac1725ba162c 121 * Broadcom Corp.
segundo 0:ac1725ba162c 122 * csm@broadcom.com
segundo 0:ac1725ba162c 123 *
segundo 0:ac1725ba162c 124 * IP checksum two bytes at a time with support for
segundo 0:ac1725ba162c 125 * unaligned buffer.
segundo 0:ac1725ba162c 126 * Works for len up to and including 0x20000.
segundo 0:ac1725ba162c 127 * by Curt McDowell, Broadcom Corp. 12/08/2005
segundo 0:ac1725ba162c 128 *
segundo 0:ac1725ba162c 129 * @param dataptr points to start of data to be summed at any boundary
segundo 0:ac1725ba162c 130 * @param len length of data to be summed
segundo 0:ac1725ba162c 131 * @return host order (!) lwip checksum (non-inverted Internet sum)
segundo 0:ac1725ba162c 132 */
segundo 0:ac1725ba162c 133
segundo 0:ac1725ba162c 134 static u16_t
segundo 0:ac1725ba162c 135 lwip_standard_chksum(void *dataptr, int len)
segundo 0:ac1725ba162c 136 {
segundo 0:ac1725ba162c 137 u8_t *pb = (u8_t *)dataptr;
segundo 0:ac1725ba162c 138 u16_t *ps, t = 0;
segundo 0:ac1725ba162c 139 u32_t sum = 0;
segundo 0:ac1725ba162c 140 int odd = ((mem_ptr_t)pb & 1);
segundo 0:ac1725ba162c 141
segundo 0:ac1725ba162c 142 /* Get aligned to u16_t */
segundo 0:ac1725ba162c 143 if (odd && len > 0) {
segundo 0:ac1725ba162c 144 ((u8_t *)&t)[1] = *pb++;
segundo 0:ac1725ba162c 145 len--;
segundo 0:ac1725ba162c 146 }
segundo 0:ac1725ba162c 147
segundo 0:ac1725ba162c 148 /* Add the bulk of the data */
segundo 0:ac1725ba162c 149 ps = (u16_t *)(void *)pb;
segundo 0:ac1725ba162c 150 while (len > 1) {
segundo 0:ac1725ba162c 151 sum += *ps++;
segundo 0:ac1725ba162c 152 len -= 2;
segundo 0:ac1725ba162c 153 }
segundo 0:ac1725ba162c 154
segundo 0:ac1725ba162c 155 /* Consume left-over byte, if any */
segundo 0:ac1725ba162c 156 if (len > 0) {
segundo 0:ac1725ba162c 157 ((u8_t *)&t)[0] = *(u8_t *)ps;
segundo 0:ac1725ba162c 158 }
segundo 0:ac1725ba162c 159
segundo 0:ac1725ba162c 160 /* Add end bytes */
segundo 0:ac1725ba162c 161 sum += t;
segundo 0:ac1725ba162c 162
segundo 0:ac1725ba162c 163 /* Fold 32-bit sum to 16 bits
segundo 0:ac1725ba162c 164 calling this twice is propably faster than if statements... */
segundo 0:ac1725ba162c 165 sum = FOLD_U32T(sum);
segundo 0:ac1725ba162c 166 sum = FOLD_U32T(sum);
segundo 0:ac1725ba162c 167
segundo 0:ac1725ba162c 168 /* Swap if alignment was odd */
segundo 0:ac1725ba162c 169 if (odd) {
segundo 0:ac1725ba162c 170 sum = SWAP_BYTES_IN_WORD(sum);
segundo 0:ac1725ba162c 171 }
segundo 0:ac1725ba162c 172
segundo 0:ac1725ba162c 173 return (u16_t)sum;
segundo 0:ac1725ba162c 174 }
segundo 0:ac1725ba162c 175 #endif
segundo 0:ac1725ba162c 176
segundo 0:ac1725ba162c 177 #if (LWIP_CHKSUM_ALGORITHM == 3) /* Alternative version #3 */
segundo 0:ac1725ba162c 178 /**
segundo 0:ac1725ba162c 179 * An optimized checksum routine. Basically, it uses loop-unrolling on
segundo 0:ac1725ba162c 180 * the checksum loop, treating the head and tail bytes specially, whereas
segundo 0:ac1725ba162c 181 * the inner loop acts on 8 bytes at a time.
segundo 0:ac1725ba162c 182 *
segundo 0:ac1725ba162c 183 * @arg start of buffer to be checksummed. May be an odd byte address.
segundo 0:ac1725ba162c 184 * @len number of bytes in the buffer to be checksummed.
segundo 0:ac1725ba162c 185 * @return host order (!) lwip checksum (non-inverted Internet sum)
segundo 0:ac1725ba162c 186 *
segundo 0:ac1725ba162c 187 * by Curt McDowell, Broadcom Corp. December 8th, 2005
segundo 0:ac1725ba162c 188 */
segundo 0:ac1725ba162c 189
segundo 0:ac1725ba162c 190 static u16_t
segundo 0:ac1725ba162c 191 lwip_standard_chksum(void *dataptr, int len)
segundo 0:ac1725ba162c 192 {
segundo 0:ac1725ba162c 193 u8_t *pb = (u8_t *)dataptr;
segundo 0:ac1725ba162c 194 u16_t *ps, t = 0;
segundo 0:ac1725ba162c 195 u32_t *pl;
segundo 0:ac1725ba162c 196 u32_t sum = 0, tmp;
segundo 0:ac1725ba162c 197 /* starts at odd byte address? */
segundo 0:ac1725ba162c 198 int odd = ((mem_ptr_t)pb & 1);
segundo 0:ac1725ba162c 199
segundo 0:ac1725ba162c 200 if (odd && len > 0) {
segundo 0:ac1725ba162c 201 ((u8_t *)&t)[1] = *pb++;
segundo 0:ac1725ba162c 202 len--;
segundo 0:ac1725ba162c 203 }
segundo 0:ac1725ba162c 204
segundo 0:ac1725ba162c 205 ps = (u16_t *)pb;
segundo 0:ac1725ba162c 206
segundo 0:ac1725ba162c 207 if (((mem_ptr_t)ps & 3) && len > 1) {
segundo 0:ac1725ba162c 208 sum += *ps++;
segundo 0:ac1725ba162c 209 len -= 2;
segundo 0:ac1725ba162c 210 }
segundo 0:ac1725ba162c 211
segundo 0:ac1725ba162c 212 pl = (u32_t *)ps;
segundo 0:ac1725ba162c 213
segundo 0:ac1725ba162c 214 while (len > 7) {
segundo 0:ac1725ba162c 215 tmp = sum + *pl++; /* ping */
segundo 0:ac1725ba162c 216 if (tmp < sum) {
segundo 0:ac1725ba162c 217 tmp++; /* add back carry */
segundo 0:ac1725ba162c 218 }
segundo 0:ac1725ba162c 219
segundo 0:ac1725ba162c 220 sum = tmp + *pl++; /* pong */
segundo 0:ac1725ba162c 221 if (sum < tmp) {
segundo 0:ac1725ba162c 222 sum++; /* add back carry */
segundo 0:ac1725ba162c 223 }
segundo 0:ac1725ba162c 224
segundo 0:ac1725ba162c 225 len -= 8;
segundo 0:ac1725ba162c 226 }
segundo 0:ac1725ba162c 227
segundo 0:ac1725ba162c 228 /* make room in upper bits */
segundo 0:ac1725ba162c 229 sum = FOLD_U32T(sum);
segundo 0:ac1725ba162c 230
segundo 0:ac1725ba162c 231 ps = (u16_t *)pl;
segundo 0:ac1725ba162c 232
segundo 0:ac1725ba162c 233 /* 16-bit aligned word remaining? */
segundo 0:ac1725ba162c 234 while (len > 1) {
segundo 0:ac1725ba162c 235 sum += *ps++;
segundo 0:ac1725ba162c 236 len -= 2;
segundo 0:ac1725ba162c 237 }
segundo 0:ac1725ba162c 238
segundo 0:ac1725ba162c 239 /* dangling tail byte remaining? */
segundo 0:ac1725ba162c 240 if (len > 0) { /* include odd byte */
segundo 0:ac1725ba162c 241 ((u8_t *)&t)[0] = *(u8_t *)ps;
segundo 0:ac1725ba162c 242 }
segundo 0:ac1725ba162c 243
segundo 0:ac1725ba162c 244 sum += t; /* add end bytes */
segundo 0:ac1725ba162c 245
segundo 0:ac1725ba162c 246 /* Fold 32-bit sum to 16 bits
segundo 0:ac1725ba162c 247 calling this twice is propably faster than if statements... */
segundo 0:ac1725ba162c 248 sum = FOLD_U32T(sum);
segundo 0:ac1725ba162c 249 sum = FOLD_U32T(sum);
segundo 0:ac1725ba162c 250
segundo 0:ac1725ba162c 251 if (odd) {
segundo 0:ac1725ba162c 252 sum = SWAP_BYTES_IN_WORD(sum);
segundo 0:ac1725ba162c 253 }
segundo 0:ac1725ba162c 254
segundo 0:ac1725ba162c 255 return (u16_t)sum;
segundo 0:ac1725ba162c 256 }
segundo 0:ac1725ba162c 257 #endif
segundo 0:ac1725ba162c 258
segundo 0:ac1725ba162c 259 /* inet_chksum_pseudo:
segundo 0:ac1725ba162c 260 *
segundo 0:ac1725ba162c 261 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
segundo 0:ac1725ba162c 262 * IP addresses are expected to be in network byte order.
segundo 0:ac1725ba162c 263 *
segundo 0:ac1725ba162c 264 * @param p chain of pbufs over that a checksum should be calculated (ip data part)
segundo 0:ac1725ba162c 265 * @param src source ip address (used for checksum of pseudo header)
segundo 0:ac1725ba162c 266 * @param dst destination ip address (used for checksum of pseudo header)
segundo 0:ac1725ba162c 267 * @param proto ip protocol (used for checksum of pseudo header)
segundo 0:ac1725ba162c 268 * @param proto_len length of the ip data part (used for checksum of pseudo header)
segundo 0:ac1725ba162c 269 * @return checksum (as u16_t) to be saved directly in the protocol header
segundo 0:ac1725ba162c 270 */
segundo 0:ac1725ba162c 271 u16_t
segundo 0:ac1725ba162c 272 inet_chksum_pseudo(struct pbuf *p,
segundo 0:ac1725ba162c 273 ip_addr_t *src, ip_addr_t *dest,
segundo 0:ac1725ba162c 274 u8_t proto, u16_t proto_len)
segundo 0:ac1725ba162c 275 {
segundo 0:ac1725ba162c 276 u32_t acc;
segundo 0:ac1725ba162c 277 u32_t addr;
segundo 0:ac1725ba162c 278 struct pbuf *q;
segundo 0:ac1725ba162c 279 u8_t swapped;
segundo 0:ac1725ba162c 280
segundo 0:ac1725ba162c 281 acc = 0;
segundo 0:ac1725ba162c 282 swapped = 0;
segundo 0:ac1725ba162c 283 /* iterate through all pbuf in chain */
segundo 0:ac1725ba162c 284 for(q = p; q != NULL; q = q->next) {
segundo 0:ac1725ba162c 285 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
segundo 0:ac1725ba162c 286 (void *)q, (void *)q->next));
segundo 0:ac1725ba162c 287 acc += LWIP_CHKSUM(q->payload, q->len);
segundo 0:ac1725ba162c 288 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
segundo 0:ac1725ba162c 289 /* just executing this next line is probably faster that the if statement needed
segundo 0:ac1725ba162c 290 to check whether we really need to execute it, and does no harm */
segundo 0:ac1725ba162c 291 acc = FOLD_U32T(acc);
segundo 0:ac1725ba162c 292 if (q->len % 2 != 0) {
segundo 0:ac1725ba162c 293 swapped = 1 - swapped;
segundo 0:ac1725ba162c 294 acc = SWAP_BYTES_IN_WORD(acc);
segundo 0:ac1725ba162c 295 }
segundo 0:ac1725ba162c 296 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
segundo 0:ac1725ba162c 297 }
segundo 0:ac1725ba162c 298
segundo 0:ac1725ba162c 299 if (swapped) {
segundo 0:ac1725ba162c 300 acc = SWAP_BYTES_IN_WORD(acc);
segundo 0:ac1725ba162c 301 }
segundo 0:ac1725ba162c 302 addr = ip4_addr_get_u32(src);
segundo 0:ac1725ba162c 303 acc += (addr & 0xffffUL);
segundo 0:ac1725ba162c 304 acc += ((addr >> 16) & 0xffffUL);
segundo 0:ac1725ba162c 305 addr = ip4_addr_get_u32(dest);
segundo 0:ac1725ba162c 306 acc += (addr & 0xffffUL);
segundo 0:ac1725ba162c 307 acc += ((addr >> 16) & 0xffffUL);
segundo 0:ac1725ba162c 308 acc += (u32_t)htons((u16_t)proto);
segundo 0:ac1725ba162c 309 acc += (u32_t)htons(proto_len);
segundo 0:ac1725ba162c 310
segundo 0:ac1725ba162c 311 /* Fold 32-bit sum to 16 bits
segundo 0:ac1725ba162c 312 calling this twice is propably faster than if statements... */
segundo 0:ac1725ba162c 313 acc = FOLD_U32T(acc);
segundo 0:ac1725ba162c 314 acc = FOLD_U32T(acc);
segundo 0:ac1725ba162c 315 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
segundo 0:ac1725ba162c 316 return (u16_t)~(acc & 0xffffUL);
segundo 0:ac1725ba162c 317 }
segundo 0:ac1725ba162c 318
segundo 0:ac1725ba162c 319 /* inet_chksum_pseudo:
segundo 0:ac1725ba162c 320 *
segundo 0:ac1725ba162c 321 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
segundo 0:ac1725ba162c 322 * IP addresses are expected to be in network byte order.
segundo 0:ac1725ba162c 323 *
segundo 0:ac1725ba162c 324 * @param p chain of pbufs over that a checksum should be calculated (ip data part)
segundo 0:ac1725ba162c 325 * @param src source ip address (used for checksum of pseudo header)
segundo 0:ac1725ba162c 326 * @param dst destination ip address (used for checksum of pseudo header)
segundo 0:ac1725ba162c 327 * @param proto ip protocol (used for checksum of pseudo header)
segundo 0:ac1725ba162c 328 * @param proto_len length of the ip data part (used for checksum of pseudo header)
segundo 0:ac1725ba162c 329 * @return checksum (as u16_t) to be saved directly in the protocol header
segundo 0:ac1725ba162c 330 */
segundo 0:ac1725ba162c 331 u16_t
segundo 0:ac1725ba162c 332 inet_chksum_pseudo_partial(struct pbuf *p,
segundo 0:ac1725ba162c 333 ip_addr_t *src, ip_addr_t *dest,
segundo 0:ac1725ba162c 334 u8_t proto, u16_t proto_len, u16_t chksum_len)
segundo 0:ac1725ba162c 335 {
segundo 0:ac1725ba162c 336 u32_t acc;
segundo 0:ac1725ba162c 337 u32_t addr;
segundo 0:ac1725ba162c 338 struct pbuf *q;
segundo 0:ac1725ba162c 339 u8_t swapped;
segundo 0:ac1725ba162c 340 u16_t chklen;
segundo 0:ac1725ba162c 341
segundo 0:ac1725ba162c 342 acc = 0;
segundo 0:ac1725ba162c 343 swapped = 0;
segundo 0:ac1725ba162c 344 /* iterate through all pbuf in chain */
segundo 0:ac1725ba162c 345 for(q = p; (q != NULL) && (chksum_len > 0); q = q->next) {
segundo 0:ac1725ba162c 346 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
segundo 0:ac1725ba162c 347 (void *)q, (void *)q->next));
segundo 0:ac1725ba162c 348 chklen = q->len;
segundo 0:ac1725ba162c 349 if (chklen > chksum_len) {
segundo 0:ac1725ba162c 350 chklen = chksum_len;
segundo 0:ac1725ba162c 351 }
segundo 0:ac1725ba162c 352 acc += LWIP_CHKSUM(q->payload, chklen);
segundo 0:ac1725ba162c 353 chksum_len -= chklen;
segundo 0:ac1725ba162c 354 LWIP_ASSERT("delete me", chksum_len < 0x7fff);
segundo 0:ac1725ba162c 355 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
segundo 0:ac1725ba162c 356 /* fold the upper bit down */
segundo 0:ac1725ba162c 357 acc = FOLD_U32T(acc);
segundo 0:ac1725ba162c 358 if (q->len % 2 != 0) {
segundo 0:ac1725ba162c 359 swapped = 1 - swapped;
segundo 0:ac1725ba162c 360 acc = SWAP_BYTES_IN_WORD(acc);
segundo 0:ac1725ba162c 361 }
segundo 0:ac1725ba162c 362 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
segundo 0:ac1725ba162c 363 }
segundo 0:ac1725ba162c 364
segundo 0:ac1725ba162c 365 if (swapped) {
segundo 0:ac1725ba162c 366 acc = SWAP_BYTES_IN_WORD(acc);
segundo 0:ac1725ba162c 367 }
segundo 0:ac1725ba162c 368 addr = ip4_addr_get_u32(src);
segundo 0:ac1725ba162c 369 acc += (addr & 0xffffUL);
segundo 0:ac1725ba162c 370 acc += ((addr >> 16) & 0xffffUL);
segundo 0:ac1725ba162c 371 addr = ip4_addr_get_u32(dest);
segundo 0:ac1725ba162c 372 acc += (addr & 0xffffUL);
segundo 0:ac1725ba162c 373 acc += ((addr >> 16) & 0xffffUL);
segundo 0:ac1725ba162c 374 acc += (u32_t)htons((u16_t)proto);
segundo 0:ac1725ba162c 375 acc += (u32_t)htons(proto_len);
segundo 0:ac1725ba162c 376
segundo 0:ac1725ba162c 377 /* Fold 32-bit sum to 16 bits
segundo 0:ac1725ba162c 378 calling this twice is propably faster than if statements... */
segundo 0:ac1725ba162c 379 acc = FOLD_U32T(acc);
segundo 0:ac1725ba162c 380 acc = FOLD_U32T(acc);
segundo 0:ac1725ba162c 381 LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
segundo 0:ac1725ba162c 382 return (u16_t)~(acc & 0xffffUL);
segundo 0:ac1725ba162c 383 }
segundo 0:ac1725ba162c 384
segundo 0:ac1725ba162c 385 /* inet_chksum:
segundo 0:ac1725ba162c 386 *
segundo 0:ac1725ba162c 387 * Calculates the Internet checksum over a portion of memory. Used primarily for IP
segundo 0:ac1725ba162c 388 * and ICMP.
segundo 0:ac1725ba162c 389 *
segundo 0:ac1725ba162c 390 * @param dataptr start of the buffer to calculate the checksum (no alignment needed)
segundo 0:ac1725ba162c 391 * @param len length of the buffer to calculate the checksum
segundo 0:ac1725ba162c 392 * @return checksum (as u16_t) to be saved directly in the protocol header
segundo 0:ac1725ba162c 393 */
segundo 0:ac1725ba162c 394
segundo 0:ac1725ba162c 395 u16_t
segundo 0:ac1725ba162c 396 inet_chksum(void *dataptr, u16_t len)
segundo 0:ac1725ba162c 397 {
segundo 0:ac1725ba162c 398 return ~LWIP_CHKSUM(dataptr, len);
segundo 0:ac1725ba162c 399 }
segundo 0:ac1725ba162c 400
segundo 0:ac1725ba162c 401 /**
segundo 0:ac1725ba162c 402 * Calculate a checksum over a chain of pbufs (without pseudo-header, much like
segundo 0:ac1725ba162c 403 * inet_chksum only pbufs are used).
segundo 0:ac1725ba162c 404 *
segundo 0:ac1725ba162c 405 * @param p pbuf chain over that the checksum should be calculated
segundo 0:ac1725ba162c 406 * @return checksum (as u16_t) to be saved directly in the protocol header
segundo 0:ac1725ba162c 407 */
segundo 0:ac1725ba162c 408 u16_t
segundo 0:ac1725ba162c 409 inet_chksum_pbuf(struct pbuf *p)
segundo 0:ac1725ba162c 410 {
segundo 0:ac1725ba162c 411 u32_t acc;
segundo 0:ac1725ba162c 412 struct pbuf *q;
segundo 0:ac1725ba162c 413 u8_t swapped;
segundo 0:ac1725ba162c 414
segundo 0:ac1725ba162c 415 acc = 0;
segundo 0:ac1725ba162c 416 swapped = 0;
segundo 0:ac1725ba162c 417 for(q = p; q != NULL; q = q->next) {
segundo 0:ac1725ba162c 418 acc += LWIP_CHKSUM(q->payload, q->len);
segundo 0:ac1725ba162c 419 acc = FOLD_U32T(acc);
segundo 0:ac1725ba162c 420 if (q->len % 2 != 0) {
segundo 0:ac1725ba162c 421 swapped = 1 - swapped;
segundo 0:ac1725ba162c 422 acc = SWAP_BYTES_IN_WORD(acc);
segundo 0:ac1725ba162c 423 }
segundo 0:ac1725ba162c 424 }
segundo 0:ac1725ba162c 425
segundo 0:ac1725ba162c 426 if (swapped) {
segundo 0:ac1725ba162c 427 acc = SWAP_BYTES_IN_WORD(acc);
segundo 0:ac1725ba162c 428 }
segundo 0:ac1725ba162c 429 return (u16_t)~(acc & 0xffffUL);
segundo 0:ac1725ba162c 430 }
segundo 0:ac1725ba162c 431
segundo 0:ac1725ba162c 432 /* These are some implementations for LWIP_CHKSUM_COPY, which copies data
segundo 0:ac1725ba162c 433 * like MEMCPY but generates a checksum at the same time. Since this is a
segundo 0:ac1725ba162c 434 * performance-sensitive function, you might want to create your own version
segundo 0:ac1725ba162c 435 * in assembly targeted at your hardware by defining it in lwipopts.h:
segundo 0:ac1725ba162c 436 * #define LWIP_CHKSUM_COPY(dst, src, len) your_chksum_copy(dst, src, len)
segundo 0:ac1725ba162c 437 */
segundo 0:ac1725ba162c 438
segundo 0:ac1725ba162c 439 #if (LWIP_CHKSUM_COPY_ALGORITHM == 1) /* Version #1 */
segundo 0:ac1725ba162c 440 /** Safe but slow: first call MEMCPY, then call LWIP_CHKSUM.
segundo 0:ac1725ba162c 441 * For architectures with big caches, data might still be in cache when
segundo 0:ac1725ba162c 442 * generating the checksum after copying.
segundo 0:ac1725ba162c 443 */
segundo 0:ac1725ba162c 444 u16_t
segundo 0:ac1725ba162c 445 lwip_chksum_copy(void *dst, const void *src, u16_t len)
segundo 0:ac1725ba162c 446 {
segundo 0:ac1725ba162c 447 MEMCPY(dst, src, len);
segundo 0:ac1725ba162c 448 return LWIP_CHKSUM(dst, len);
segundo 0:ac1725ba162c 449 }
segundo 0:ac1725ba162c 450 #endif /* (LWIP_CHKSUM_COPY_ALGORITHM == 1) */