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