A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers inet_chksum.c Source File

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 #include <string.h>
00045 
00046 /* These are some reference implementations of the checksum algorithm, with the
00047  * aim of being simple, correct and fully portable. Checksumming is the
00048  * first thing you would want to optimize for your platform. If you create
00049  * your own version, link it in and in your cc.h put:
00050  * 
00051  * #define LWIP_CHKSUM <your_checksum_routine> 
00052  *
00053  * Or you can select from the implementations below by defining
00054  * LWIP_CHKSUM_ALGORITHM to 1, 2 or 3.
00055  */
00056 
00057 #ifndef LWIP_CHKSUM
00058 # define LWIP_CHKSUM lwip_standard_chksum
00059 # ifndef LWIP_CHKSUM_ALGORITHM
00060 #  define LWIP_CHKSUM_ALGORITHM 1
00061 # endif
00062 #endif
00063 /* If none set: */
00064 #ifndef LWIP_CHKSUM_ALGORITHM
00065 # define LWIP_CHKSUM_ALGORITHM 0
00066 #endif
00067 
00068 #if (LWIP_CHKSUM_ALGORITHM == 1) /* Version #1 */
00069 /**
00070  * lwip checksum
00071  *
00072  * @param dataptr points to start of data to be summed at any boundary
00073  * @param len length of data to be summed
00074  * @return host order (!) lwip checksum (non-inverted Internet sum) 
00075  *
00076  * @note accumulator size limits summable length to 64k
00077  * @note host endianess is irrelevant (p3 RFC1071)
00078  */
00079 static u16_t
00080 lwip_standard_chksum(void *dataptr, u16_t len)
00081 {
00082   u32_t acc;
00083   u16_t src;
00084   u8_t *octetptr;
00085 
00086   acc = 0;
00087   /* dataptr may be at odd or even addresses */
00088   octetptr = (u8_t*)dataptr;
00089   while (len > 1)
00090   {
00091     /* declare first octet as most significant
00092        thus assume network order, ignoring host order */
00093     src = (*octetptr) << 8;
00094     octetptr++;
00095     /* declare second octet as least significant */
00096     src |= (*octetptr);
00097     octetptr++;
00098     acc += src;
00099     len -= 2;
00100   }
00101   if (len > 0)
00102   {
00103     /* accumulate remaining octet */
00104     src = (*octetptr) << 8;
00105     acc += src;
00106   }
00107   /* add deferred carry bits */
00108   acc = (acc >> 16) + (acc & 0x0000ffffUL);
00109   if ((acc & 0xffff0000) != 0) {
00110     acc = (acc >> 16) + (acc & 0x0000ffffUL);
00111   }
00112   /* This maybe a little confusing: reorder sum using htons()
00113      instead of ntohs() since it has a little less call overhead.
00114      The caller must invert bits for Internet sum ! */
00115   return htons((u16_t)acc);
00116 }
00117 #endif
00118 
00119 #if (LWIP_CHKSUM_ALGORITHM == 2) /* Alternative version #2 */
00120 /*
00121  * Curt McDowell
00122  * Broadcom Corp.
00123  * csm@broadcom.com
00124  *
00125  * IP checksum two bytes at a time with support for
00126  * unaligned buffer.
00127  * Works for len up to and including 0x20000.
00128  * by Curt McDowell, Broadcom Corp. 12/08/2005
00129  *
00130  * @param dataptr points to start of data to be summed at any boundary
00131  * @param len length of data to be summed
00132  * @return host order (!) lwip checksum (non-inverted Internet sum) 
00133  */
00134 
00135 static u16_t
00136 lwip_standard_chksum(void *dataptr, int len)
00137 {
00138   u8_t *pb = dataptr;
00139   u16_t *ps, t = 0;
00140   u32_t sum = 0;
00141   int odd = ((u32_t)pb & 1);
00142 
00143   /* Get aligned to u16_t */
00144   if (odd && len > 0) {
00145     ((u8_t *)&t)[1] = *pb++;
00146     len--;
00147   }
00148 
00149   /* Add the bulk of the data */
00150   ps = (u16_t *)pb;
00151   while (len > 1) {
00152     sum += *ps++;
00153     len -= 2;
00154   }
00155 
00156   /* Consume left-over byte, if any */
00157   if (len > 0)
00158     ((u8_t *)&t)[0] = *(u8_t *)ps;;
00159 
00160   /* Add end bytes */
00161   sum += t;
00162 
00163   /*  Fold 32-bit sum to 16 bits */
00164   while ((sum >> 16) != 0)
00165     sum = (sum & 0xffff) + (sum >> 16);
00166 
00167   /* Swap if alignment was odd */
00168   if (odd)
00169     sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8);
00170 
00171   return sum;
00172 }
00173 #endif
00174 
00175 #if (LWIP_CHKSUM_ALGORITHM == 3) /* Alternative version #3 */
00176 /**
00177  * An optimized checksum routine. Basically, it uses loop-unrolling on
00178  * the checksum loop, treating the head and tail bytes specially, whereas
00179  * the inner loop acts on 8 bytes at a time. 
00180  *
00181  * @arg start of buffer to be checksummed. May be an odd byte address.
00182  * @len number of bytes in the buffer to be checksummed.
00183  * @return host order (!) lwip checksum (non-inverted Internet sum) 
00184  * 
00185  * by Curt McDowell, Broadcom Corp. December 8th, 2005
00186  */
00187 
00188 static u16_t
00189 lwip_standard_chksum(void *dataptr, int len)
00190 {
00191   u8_t *pb = dataptr;
00192   u16_t *ps, t = 0;
00193   u32_t *pl;
00194   u32_t sum = 0, tmp;
00195   /* starts at odd byte address? */
00196   int odd = ((u32_t)pb & 1);
00197 
00198   if (odd && len > 0) {
00199     ((u8_t *)&t)[1] = *pb++;
00200     len--;
00201   }
00202 
00203   ps = (u16_t *)pb;
00204 
00205   if (((u32_t)ps & 3) && len > 1) {
00206     sum += *ps++;
00207     len -= 2;
00208   }
00209 
00210   pl = (u32_t *)ps;
00211 
00212   while (len > 7)  {
00213     tmp = sum + *pl++;          /* ping */
00214     if (tmp < sum)
00215       tmp++;                    /* add back carry */
00216 
00217     sum = tmp + *pl++;          /* pong */
00218     if (sum < tmp)
00219       sum++;                    /* add back carry */
00220 
00221     len -= 8;
00222   }
00223 
00224   /* make room in upper bits */
00225   sum = (sum >> 16) + (sum & 0xffff);
00226 
00227   ps = (u16_t *)pl;
00228 
00229   /* 16-bit aligned word remaining? */
00230   while (len > 1) {
00231     sum += *ps++;
00232     len -= 2;
00233   }
00234 
00235   /* dangling tail byte remaining? */
00236   if (len > 0)                  /* include odd byte */
00237     ((u8_t *)&t)[0] = *(u8_t *)ps;
00238 
00239   sum += t;                     /* add end bytes */
00240 
00241   while ((sum >> 16) != 0)      /* combine halves */
00242     sum = (sum >> 16) + (sum & 0xffff);
00243 
00244   if (odd)
00245     sum = ((sum & 0xff) << 8) | ((sum & 0xff00) >> 8);
00246 
00247   return sum;
00248 }
00249 #endif
00250 
00251 /* inet_chksum_pseudo:
00252  *
00253  * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
00254  * IP addresses are expected to be in network byte order.
00255  *
00256  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
00257  * @param src source ip address (used for checksum of pseudo header)
00258  * @param dst destination ip address (used for checksum of pseudo header)
00259  * @param proto ip protocol (used for checksum of pseudo header)
00260  * @param proto_len length of the ip data part (used for checksum of pseudo header)
00261  * @return checksum (as u16_t) to be saved directly in the protocol header
00262  */
00263 u16_t
00264 inet_chksum_pseudo(struct pbuf *p,
00265        struct ip_addr *src, struct ip_addr *dest,
00266        u8_t proto, u16_t proto_len)
00267 {
00268   u32_t acc;
00269   struct pbuf *q;
00270   u8_t swapped;
00271 
00272   acc = 0;
00273   swapped = 0;
00274   /* iterate through all pbuf in chain */
00275   for(q = p; q != NULL; q = q->next) {
00276     LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
00277       (void *)q, (void *)q->next));
00278     acc += LWIP_CHKSUM(q->payload, q->len);
00279     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
00280     while ((acc >> 16) != 0) {
00281       acc = (acc & 0xffffUL) + (acc >> 16);
00282     }
00283     if (q->len % 2 != 0) {
00284       swapped = 1 - swapped;
00285       acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
00286     }
00287     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
00288   }
00289 
00290   if (swapped) {
00291     acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
00292   }
00293   acc += (src->addr & 0xffffUL);
00294   acc += ((src->addr >> 16) & 0xffffUL);
00295   acc += (dest->addr & 0xffffUL);
00296   acc += ((dest->addr >> 16) & 0xffffUL);
00297   acc += (u32_t)htons((u16_t)proto);
00298   acc += (u32_t)htons(proto_len);
00299 
00300   while ((acc >> 16) != 0) {
00301     acc = (acc & 0xffffUL) + (acc >> 16);
00302   }
00303   LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
00304   return (u16_t)~(acc & 0xffffUL);
00305 }
00306 
00307 /* inet_chksum_pseudo:
00308  *
00309  * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
00310  * IP addresses are expected to be in network byte order.
00311  *
00312  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
00313  * @param src source ip address (used for checksum of pseudo header)
00314  * @param dst destination ip address (used for checksum of pseudo header)
00315  * @param proto ip protocol (used for checksum of pseudo header)
00316  * @param proto_len length of the ip data part (used for checksum of pseudo header)
00317  * @return checksum (as u16_t) to be saved directly in the protocol header
00318  */
00319 u16_t
00320 inet_chksum_pseudo_partial(struct pbuf *p,
00321        struct ip_addr *src, struct ip_addr *dest,
00322        u8_t proto, u16_t proto_len, u16_t chksum_len)
00323 {
00324   u32_t acc;
00325   struct pbuf *q;
00326   u8_t swapped;
00327   u16_t chklen;
00328 
00329   acc = 0;
00330   swapped = 0;
00331   /* iterate through all pbuf in chain */
00332   for(q = p; (q != NULL) && (chksum_len > 0); q = q->next) {
00333     LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
00334       (void *)q, (void *)q->next));
00335     chklen = q->len;
00336     if (chklen > chksum_len) {
00337       chklen = chksum_len;
00338     }
00339     acc += LWIP_CHKSUM(q->payload, chklen);
00340     chksum_len -= chklen;
00341     LWIP_ASSERT("delete me", chksum_len < 0x7fff);
00342     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
00343     while ((acc >> 16) != 0) {
00344       acc = (acc & 0xffffUL) + (acc >> 16);
00345     }
00346     if (q->len % 2 != 0) {
00347       swapped = 1 - swapped;
00348       acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
00349     }
00350     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
00351   }
00352 
00353   if (swapped) {
00354     acc = ((acc & 0xff) << 8) | ((acc & 0xff00UL) >> 8);
00355   }
00356   acc += (src->addr & 0xffffUL);
00357   acc += ((src->addr >> 16) & 0xffffUL);
00358   acc += (dest->addr & 0xffffUL);
00359   acc += ((dest->addr >> 16) & 0xffffUL);
00360   acc += (u32_t)htons((u16_t)proto);
00361   acc += (u32_t)htons(proto_len);
00362 
00363   while ((acc >> 16) != 0) {
00364     acc = (acc & 0xffffUL) + (acc >> 16);
00365   }
00366   LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
00367   return (u16_t)~(acc & 0xffffUL);
00368 }
00369 
00370 /* inet_chksum:
00371  *
00372  * Calculates the Internet checksum over a portion of memory. Used primarily for IP
00373  * and ICMP.
00374  *
00375  * @param dataptr start of the buffer to calculate the checksum (no alignment needed)
00376  * @param len length of the buffer to calculate the checksum
00377  * @return checksum (as u16_t) to be saved directly in the protocol header
00378  */
00379 
00380 u16_t
00381 inet_chksum(void *dataptr, u16_t len)
00382 {
00383   u32_t acc;
00384 
00385   acc = LWIP_CHKSUM(dataptr, len);
00386   while ((acc >> 16) != 0) {
00387     acc = (acc & 0xffff) + (acc >> 16);
00388   }
00389   return (u16_t)~(acc & 0xffff);
00390 }
00391 
00392 /**
00393  * Calculate a checksum over a chain of pbufs (without pseudo-header, much like
00394  * inet_chksum only pbufs are used).
00395  *
00396  * @param p pbuf chain over that the checksum should be calculated
00397  * @return checksum (as u16_t) to be saved directly in the protocol header
00398  */
00399 u16_t
00400 inet_chksum_pbuf(struct pbuf *p)
00401 {
00402   u32_t acc;
00403   struct pbuf *q;
00404   u8_t swapped;
00405 
00406   acc = 0;
00407   swapped = 0;
00408   for(q = p; q != NULL; q = q->next) {
00409     acc += LWIP_CHKSUM(q->payload, q->len);
00410     while ((acc >> 16) != 0) {
00411       acc = (acc & 0xffffUL) + (acc >> 16);
00412     }
00413     if (q->len % 2 != 0) {
00414       swapped = 1 - swapped;
00415       acc = (acc & 0x00ffUL << 8) | (acc & 0xff00UL >> 8);
00416     }
00417   }
00418 
00419   if (swapped) {
00420     acc = ((acc & 0x00ffUL) << 8) | ((acc & 0xff00UL) >> 8);
00421   }
00422   return (u16_t)~(acc & 0xffffUL);
00423 }