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