• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * 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 <string.h>
55 
56 #ifndef LWIP_CHKSUM
57 # define LWIP_CHKSUM lwip_standard_chksum
58 # ifndef LWIP_CHKSUM_ALGORITHM
59 #  define LWIP_CHKSUM_ALGORITHM 2
60 # endif
61 u16_t lwip_standard_chksum(const void *dataptr, int len);
62 #endif
63 /* If none set: */
64 #ifndef LWIP_CHKSUM_ALGORITHM
65 # define LWIP_CHKSUM_ALGORITHM 0
66 #endif
67 
68 #if (LWIP_CHKSUM_ALGORITHM == 1) /* Version #1 */
69 /**
70  * lwip checksum
71  *
72  * @param dataptr points to start of data to be summed at any boundary
73  * @param len length of data to be summed
74  * @return host order (!) lwip checksum (non-inverted Internet sum)
75  *
76  * @note accumulator size limits summable length to 64k
77  * @note host endianess is irrelevant (p3 RFC1071)
78  */
79 u16_t
lwip_standard_chksum(const void * dataptr,int len)80 lwip_standard_chksum(const void *dataptr, int len)
81 {
82   u32_t acc;
83   u16_t src;
84   const u8_t *octetptr;
85 
86   acc = 0;
87   /* dataptr may be at odd or even addresses */
88   octetptr = (const u8_t *)dataptr;
89   while (len > 1) {
90     /* declare first octet as most significant
91        thus assume network order, ignoring host order */
92     src = (*octetptr) << 8;
93     octetptr++;
94     /* declare second octet as least significant */
95     src |= (*octetptr);
96     octetptr++;
97     acc += src;
98     len -= 2;
99   }
100   if (len > 0) {
101     /* accumulate remaining octet */
102     src = (*octetptr) << 8;
103     acc += src;
104   }
105   /* add deferred carry bits */
106   acc = (acc >> 16) + (acc & 0x0000ffffUL);
107   if ((acc & 0xffff0000UL) != 0) {
108     acc = (acc >> 16) + (acc & 0x0000ffffUL);
109   }
110   /* This maybe a little confusing: reorder sum using lwip_htons()
111      instead of lwip_ntohs() since it has a little less call overhead.
112      The caller must invert bits for Internet sum ! */
113   return lwip_htons((u16_t)acc);
114 }
115 #endif
116 
117 #if (LWIP_CHKSUM_ALGORITHM == 2) /* Alternative version #2 */
118 /*
119  * Curt McDowell
120  * Broadcom Corp.
121  * csm@broadcom.com
122  *
123  * IP checksum two bytes at a time with support for
124  * unaligned buffer.
125  * Works for len up to and including 0x20000.
126  * by Curt McDowell, Broadcom Corp. 12/08/2005
127  *
128  * @param dataptr points to start of data to be summed at any boundary
129  * @param len length of data to be summed
130  * @return host order (!) lwip checksum (non-inverted Internet sum)
131  */
132 u16_t
lwip_standard_chksum(const void * dataptr,int len)133 lwip_standard_chksum(const void *dataptr, int len)
134 {
135   const u8_t *pb = (const u8_t *)dataptr;
136   const u16_t *ps;
137   u16_t t = 0;
138   u32_t sum = 0;
139   int odd = ((mem_ptr_t)pb & 1);
140 
141   /* Get aligned to u16_t */
142   if (odd && len > 0) {
143     ((u8_t *)&t)[1] = *pb++;
144     len--;
145   }
146 
147   /* Add the bulk of the data */
148   ps = (const u16_t *)(const void *)pb;
149   while (len > 1) {
150     sum += *ps++;
151     len -= 2;
152   }
153 
154   /* Consume left-over byte, if any */
155   if (len > 0) {
156     ((u8_t *)&t)[0] = *(const u8_t *)ps;
157   }
158 
159   /* Add end bytes */
160   sum += t;
161 
162   /* Fold 32-bit sum to 16 bits
163      calling this twice is probably faster than if statements... */
164   sum = FOLD_U32T(sum);
165   sum = FOLD_U32T(sum);
166 
167   /* Swap if alignment was odd */
168   if (odd) {
169     sum = SWAP_BYTES_IN_WORD(sum);
170   }
171 
172   return (u16_t)sum;
173 }
174 #endif
175 
176 #if (LWIP_CHKSUM_ALGORITHM == 3) /* Alternative version #3 */
177 /**
178  * An optimized checksum routine. Basically, it uses loop-unrolling on
179  * the checksum loop, treating the head and tail bytes specially, whereas
180  * the inner loop acts on 8 bytes at a time.
181  *
182  * @arg start of buffer to be checksummed. May be an odd byte address.
183  * @len number of bytes in the buffer to be checksummed.
184  * @return host order (!) lwip checksum (non-inverted Internet sum)
185  *
186  * by Curt McDowell, Broadcom Corp. December 8th, 2005
187  */
188 u16_t
lwip_standard_chksum(const void * dataptr,int len)189 lwip_standard_chksum(const void *dataptr, int len)
190 {
191   const u8_t *pb = (const u8_t *)dataptr;
192   const u16_t *ps;
193   u16_t t = 0;
194   const u32_t *pl;
195   u32_t sum = 0, tmp;
196   /* starts at odd byte address? */
197   int odd = ((mem_ptr_t)pb & 1);
198 
199   if (odd && len > 0) {
200     ((u8_t *)&t)[1] = *pb++;
201     len--;
202   }
203 
204   ps = (const u16_t *)(const void *)pb;
205 
206   if (((mem_ptr_t)ps & 3) && len > 1) {
207     sum += *ps++;
208     len -= 2;
209   }
210 
211   pl = (const u32_t *)(const void *)ps;
212 
213   while (len > 7)  {
214     tmp = sum + *pl++;          /* ping */
215     if (tmp < sum) {
216       tmp++;                    /* add back carry */
217     }
218 
219     sum = tmp + *pl++;          /* pong */
220     if (sum < tmp) {
221       sum++;                    /* add back carry */
222     }
223 
224     len -= 8;
225   }
226 
227   /* make room in upper bits */
228   sum = FOLD_U32T(sum);
229 
230   ps = (const u16_t *)pl;
231 
232   /* 16-bit aligned word remaining? */
233   while (len > 1) {
234     sum += *ps++;
235     len -= 2;
236   }
237 
238   /* dangling tail byte remaining? */
239   if (len > 0) {                /* include odd byte */
240     ((u8_t *)&t)[0] = *(const u8_t *)ps;
241   }
242 
243   sum += t;                     /* add end bytes */
244 
245   /* Fold 32-bit sum to 16 bits
246      calling this twice is probably faster than if statements... */
247   sum = FOLD_U32T(sum);
248   sum = FOLD_U32T(sum);
249 
250   if (odd) {
251     sum = SWAP_BYTES_IN_WORD(sum);
252   }
253 
254   return (u16_t)sum;
255 }
256 #endif
257 
258 #if (LWIP_CHKSUM_ALGORITHM == 4) /* version #4, asm based */
259 
260 extern unsigned short in_cksum(const void *buf, int len);
261 u16_t
lwip_standard_chksum(const void * dataptr,int len)262 lwip_standard_chksum(const void *dataptr, int len)
263 {
264   return ~(u16_t)(in_cksum(dataptr, len));
265 }
266 #endif
267 
268 #if (LWIP_CHKSUM_ALGORITHM == 5) /* version #5, asm based */
269 #include "checksum.h"
270 u16_t
lwip_standard_chksum(const void * dataptr,int len)271 lwip_standard_chksum(const void *dataptr, int len)
272 {
273   return (u16_t) ~(csum_fold(csum_partial(dataptr, len, 0)));
274 }
275 #endif
276 
277 /** Parts of the pseudo checksum which are common to IPv4 and IPv6 */
278 static u16_t
inet_cksum_pseudo_base(struct pbuf * p,u8_t proto,u16_t proto_len,u32_t acc)279 inet_cksum_pseudo_base(struct pbuf *p, u8_t proto, u16_t proto_len, u32_t acc)
280 {
281   struct pbuf *q;
282   int swapped = 0;
283 
284   /* iterate through all pbuf in chain */
285   for (q = p; q != NULL; q = q->next) {
286     LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
287                              (void *)q, (void *)q->next));
288     acc += LWIP_CHKSUM(q->payload, q->len);
289     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
290     /* just executing this next line is probably faster that the if statement needed
291        to check whether we really need to execute it, and does no harm */
292     acc = FOLD_U32T(acc);
293     if (q->len % 2 != 0) {
294       swapped = !swapped;
295       acc = SWAP_BYTES_IN_WORD(acc);
296     }
297     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
298   }
299 
300   if (swapped) {
301     acc = SWAP_BYTES_IN_WORD(acc);
302   }
303 
304   acc += (u32_t)lwip_htons((u16_t)proto);
305   acc += (u32_t)lwip_htons(proto_len);
306 
307   /* Fold 32-bit sum to 16 bits
308      calling this twice is probably faster than if statements... */
309   acc = FOLD_U32T(acc);
310   acc = FOLD_U32T(acc);
311   LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
312   return (u16_t)~(acc & 0xffffUL);
313 }
314 
315 #if LWIP_IPV4
316 /* inet_chksum_pseudo:
317  *
318  * Calculates the IPv4 pseudo Internet checksum used by TCP and UDP for a pbuf chain.
319  * IP addresses are expected to be in network byte order.
320  *
321  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
322  * @param src source ip address (used for checksum of pseudo header)
323  * @param dst destination ip address (used for checksum of pseudo header)
324  * @param proto ip protocol (used for checksum of pseudo header)
325  * @param proto_len length of the ip data part (used for checksum of pseudo header)
326  * @return checksum (as u16_t) to be saved directly in the protocol header
327  */
328 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)329 inet_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
330                    const ip4_addr_t *src, const ip4_addr_t *dest)
331 {
332   u32_t acc;
333   u32_t addr;
334 
335   addr = ip4_addr_get_u32(src);
336   acc = (addr & 0xffffUL);
337   acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
338   addr = ip4_addr_get_u32(dest);
339   acc = (u32_t)(acc + (addr & 0xffffUL));
340   acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
341   /* fold down to 16 bits */
342   acc = FOLD_U32T(acc);
343   acc = FOLD_U32T(acc);
344 
345   return inet_cksum_pseudo_base(p, proto, proto_len, acc);
346 }
347 #endif /* LWIP_IPV4 */
348 
349 #if LWIP_IPV6
350 /**
351  * Calculates the checksum with IPv6 pseudo header used by TCP and UDP for a pbuf chain.
352  * IPv6 addresses are expected to be in network byte order.
353  *
354  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
355  * @param proto ipv6 protocol/next header (used for checksum of pseudo header)
356  * @param proto_len length of the ipv6 payload (used for checksum of pseudo header)
357  * @param src source ipv6 address (used for checksum of pseudo header)
358  * @param dest destination ipv6 address (used for checksum of pseudo header)
359  * @return checksum (as u16_t) to be saved directly in the protocol header
360  */
361 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)362 ip6_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
363                   const ip6_addr_t *src, const ip6_addr_t *dest)
364 {
365   u32_t acc = 0;
366   u32_t addr;
367   u8_t addr_part;
368 
369   for (addr_part = 0; addr_part < 4; addr_part++) {
370     addr = src->addr[addr_part];
371     acc = (u32_t)(acc + (addr & 0xffffUL));
372     acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
373     addr = dest->addr[addr_part];
374     acc = (u32_t)(acc + (addr & 0xffffUL));
375     acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
376   }
377   /* fold down to 16 bits */
378   acc = FOLD_U32T(acc);
379   acc = FOLD_U32T(acc);
380 
381   return inet_cksum_pseudo_base(p, proto, proto_len, acc);
382 }
383 #endif /* LWIP_IPV6 */
384 
385 /* ip_chksum_pseudo:
386  *
387  * Calculates the IPv4 or IPv6 pseudo Internet checksum used by TCP and UDP for a pbuf chain.
388  * IP addresses are expected to be in network byte order.
389  *
390  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
391  * @param src source ip address (used for checksum of pseudo header)
392  * @param dst destination ip address (used for checksum of pseudo header)
393  * @param proto ip protocol (used for checksum of pseudo header)
394  * @param proto_len length of the ip data part (used for checksum of pseudo header)
395  * @return checksum (as u16_t) to be saved directly in the protocol header
396  */
397 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)398 ip_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
399                  const ip_addr_t *src, const ip_addr_t *dest)
400 {
401 #if LWIP_IPV6
402   if (IP_IS_V6_VAL(*dest)) {
403     return ip6_chksum_pseudo(p, proto, proto_len, ip_2_ip6(src), ip_2_ip6(dest));
404   }
405 #endif /* LWIP_IPV6 */
406 #if LWIP_IPV4 && LWIP_IPV6
407   else
408 #endif /* LWIP_IPV4 && LWIP_IPV6 */
409 #if LWIP_IPV4
410   {
411     return inet_chksum_pseudo(p, proto, proto_len, ip_2_ip4(src), ip_2_ip4(dest));
412   }
413 #endif /* LWIP_IPV4 */
414 }
415 
416 /** Parts of the pseudo checksum which are common to IPv4 and IPv6 */
417 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)418 inet_cksum_pseudo_partial_base(struct pbuf *p, u8_t proto, u16_t proto_len,
419                                u16_t chksum_len, u32_t acc)
420 {
421   struct pbuf *q;
422   int swapped = 0;
423   u16_t chklen;
424 
425   /* iterate through all pbuf in chain */
426   for (q = p; (q != NULL) && (chksum_len > 0); q = q->next) {
427     LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
428                              (void *)q, (void *)q->next));
429     chklen = q->len;
430     if (chklen > chksum_len) {
431       chklen = chksum_len;
432     }
433     acc += LWIP_CHKSUM(q->payload, chklen);
434     chksum_len = (u16_t)(chksum_len - chklen);
435     LWIP_ASSERT("delete me", chksum_len < 0x7fff);
436     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
437     /* fold the upper bit down */
438     acc = FOLD_U32T(acc);
439     if (q->len % 2 != 0) {
440       swapped = !swapped;
441       acc = SWAP_BYTES_IN_WORD(acc);
442     }
443     /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
444   }
445 
446   if (swapped) {
447     acc = SWAP_BYTES_IN_WORD(acc);
448   }
449 
450   acc += (u32_t)lwip_htons((u16_t)proto);
451   acc += (u32_t)lwip_htons(proto_len);
452 
453   /* Fold 32-bit sum to 16 bits
454      calling this twice is probably faster than if statements... */
455   acc = FOLD_U32T(acc);
456   acc = FOLD_U32T(acc);
457   LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F"\n", acc));
458   return (u16_t)~(acc & 0xffffUL);
459 }
460 
461 #if LWIP_IPV4
462 /* inet_chksum_pseudo_partial:
463  *
464  * Calculates the IPv4 pseudo Internet checksum used by TCP and UDP for a pbuf chain.
465  * IP addresses are expected to be in network byte order.
466  *
467  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
468  * @param src source ip address (used for checksum of pseudo header)
469  * @param dst destination ip address (used for checksum of pseudo header)
470  * @param proto ip protocol (used for checksum of pseudo header)
471  * @param proto_len length of the ip data part (used for checksum of pseudo header)
472  * @return checksum (as u16_t) to be saved directly in the protocol header
473  */
474 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)475 inet_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len,
476                            u16_t chksum_len, const ip4_addr_t *src, const ip4_addr_t *dest)
477 {
478   u32_t acc;
479   u32_t addr;
480 
481   addr = ip4_addr_get_u32(src);
482   acc = (addr & 0xffffUL);
483   acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
484   addr = ip4_addr_get_u32(dest);
485   acc = (u32_t)(acc + (addr & 0xffffUL));
486   acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
487   /* fold down to 16 bits */
488   acc = FOLD_U32T(acc);
489   acc = FOLD_U32T(acc);
490 
491   return inet_cksum_pseudo_partial_base(p, proto, proto_len, chksum_len, acc);
492 }
493 #endif /* LWIP_IPV4 */
494 
495 #if LWIP_IPV6
496 /**
497  * Calculates the checksum with IPv6 pseudo header used by TCP and UDP for a pbuf chain.
498  * IPv6 addresses are expected to be in network byte order. Will only compute for a
499  * portion of the payload.
500  *
501  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
502  * @param proto ipv6 protocol/next header (used for checksum of pseudo header)
503  * @param proto_len length of the ipv6 payload (used for checksum of pseudo header)
504  * @param chksum_len number of payload bytes used to compute chksum
505  * @param src source ipv6 address (used for checksum of pseudo header)
506  * @param dest destination ipv6 address (used for checksum of pseudo header)
507  * @return checksum (as u16_t) to be saved directly in the protocol header
508  */
509 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)510 ip6_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len,
511                           u16_t chksum_len, const ip6_addr_t *src, const ip6_addr_t *dest)
512 {
513   u32_t acc = 0;
514   u32_t addr;
515   u8_t addr_part;
516 
517   for (addr_part = 0; addr_part < 4; addr_part++) {
518     addr = src->addr[addr_part];
519     acc = (u32_t)(acc + (addr & 0xffffUL));
520     acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
521     addr = dest->addr[addr_part];
522     acc = (u32_t)(acc + (addr & 0xffffUL));
523     acc = (u32_t)(acc + ((addr >> 16) & 0xffffUL));
524   }
525   /* fold down to 16 bits */
526   acc = FOLD_U32T(acc);
527   acc = FOLD_U32T(acc);
528 
529   return inet_cksum_pseudo_partial_base(p, proto, proto_len, chksum_len, acc);
530 }
531 #endif /* LWIP_IPV6 */
532 
533 /* ip_chksum_pseudo_partial:
534  *
535  * Calculates the IPv4 or IPv6 pseudo Internet checksum used by TCP and UDP for a pbuf chain.
536  *
537  * @param p chain of pbufs over that a checksum should be calculated (ip data part)
538  * @param src source ip address (used for checksum of pseudo header)
539  * @param dst destination ip address (used for checksum of pseudo header)
540  * @param proto ip protocol (used for checksum of pseudo header)
541  * @param proto_len length of the ip data part (used for checksum of pseudo header)
542  * @return checksum (as u16_t) to be saved directly in the protocol header
543  */
544 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)545 ip_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len,
546                          u16_t chksum_len, const ip_addr_t *src, const ip_addr_t *dest)
547 {
548 #if LWIP_IPV6
549   if (IP_IS_V6_VAL(*dest)) {
550     return ip6_chksum_pseudo_partial(p, proto, proto_len, chksum_len, ip_2_ip6(src), ip_2_ip6(dest));
551   }
552 #endif /* LWIP_IPV6 */
553 #if LWIP_IPV4 && LWIP_IPV6
554   else
555 #endif /* LWIP_IPV4 && LWIP_IPV6 */
556 #if LWIP_IPV4
557   {
558     return inet_chksum_pseudo_partial(p, proto, proto_len, chksum_len, ip_2_ip4(src), ip_2_ip4(dest));
559   }
560 #endif /* LWIP_IPV4 */
561 }
562 
563 /* inet_chksum:
564  *
565  * Calculates the Internet checksum over a portion of memory. Used primarily for IP
566  * and ICMP.
567  *
568  * @param dataptr start of the buffer to calculate the checksum (no alignment needed)
569  * @param len length of the buffer to calculate the checksum
570  * @return checksum (as u16_t) to be saved directly in the protocol header
571  */
572 
573 u16_t
inet_chksum(const void * dataptr,u16_t len)574 inet_chksum(const void *dataptr, u16_t len)
575 {
576   return (u16_t)~(unsigned int)LWIP_CHKSUM(dataptr, len);
577 }
578 
579 /**
580  * Calculate a checksum over a chain of pbufs (without pseudo-header, much like
581  * inet_chksum only pbufs are used).
582  *
583  * @param p pbuf chain over that the checksum should be calculated
584  * @return checksum (as u16_t) to be saved directly in the protocol header
585  */
586 u16_t
inet_chksum_pbuf(struct pbuf * p)587 inet_chksum_pbuf(struct pbuf *p)
588 {
589   u32_t acc;
590   struct pbuf *q;
591   int swapped = 0;
592 
593   acc = 0;
594   for (q = p; q != NULL; q = q->next) {
595     acc += LWIP_CHKSUM(q->payload, q->len);
596     acc = FOLD_U32T(acc);
597     if (q->len % 2 != 0) {
598       swapped = !swapped;
599       acc = SWAP_BYTES_IN_WORD(acc);
600     }
601   }
602 
603   if (swapped) {
604     acc = SWAP_BYTES_IN_WORD(acc);
605   }
606   return (u16_t)~(acc & 0xffffUL);
607 }
608 
609 /* These are some implementations for LWIP_CHKSUM_COPY, which copies data
610  * like MEMCPY but generates a checksum at the same time. Since this is a
611  * performance-sensitive function, you might want to create your own version
612  * in assembly targeted at your hardware by defining it in lwipopts.h:
613  *   #define LWIP_CHKSUM_COPY(dst, src, len) your_chksum_copy(dst, src, len)
614  */
615 
616 #if (LWIP_CHKSUM_COPY_ALGORITHM == 1) /* Version #1 */
617 /** Safe but slow: first call MEMCPY, then call LWIP_CHKSUM.
618  * For architectures with big caches, data might still be in cache when
619  * generating the checksum after copying.
620  */
621 u16_t
lwip_chksum_copy(void * dst,const void * src,u16_t len)622 lwip_chksum_copy(void *dst, const void *src, u16_t len)
623 {
624   MEMCPY(dst, src, len);
625   return LWIP_CHKSUM(dst, len);
626 }
627 #elif (LWIP_CHKSUM_COPY_ALGORITHM == 2)
lwip_chksum_copy(void * dst,const void * src,u16_t len)628 u16_t lwip_chksum_copy(void *dst, const void *src, u16_t len)
629 {
630   return ~(u16_t)(in_cksum_copy(src, dst, len));
631 }
632 #endif /* (LWIP_CHKSUM_COPY_ALGORITHM == 1) */
633