3 * Incluse internet checksum functions.
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
41 #include "lwip/inet_chksum.h"
47 /* These are some reference implementations of the checksum algorithm, with the
48 * aim of being simple, correct and fully portable. Checksumming is the
49 * first thing you would want to optimize for your platform. If you create
50 * your own version, link it in and in your cc.h put:
52 * #define LWIP_CHKSUM <your_checksum_routine>
54 * Or you can select from the implementations below by defining
55 * LWIP_CHKSUM_ALGORITHM to 1, 2 or 3.
59 # define LWIP_CHKSUM lwip_standard_chksum
60 # ifndef LWIP_CHKSUM_ALGORITHM
61 # define LWIP_CHKSUM_ALGORITHM 2
65 #ifndef LWIP_CHKSUM_ALGORITHM
66 # define LWIP_CHKSUM_ALGORITHM 0
69 #if (LWIP_CHKSUM_ALGORITHM == 1) /* Version #1 */
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)
77 * @note accumulator size limits summable length to 64k
78 * @note host endianess is irrelevant (p3 RFC1071)
81 lwip_standard_chksum(void *dataptr
, u16_t len
)
88 /* dataptr may be at odd or even addresses */
89 octetptr
= (u8_t
*)dataptr
;
91 /* declare first octet as most significant
92 thus assume network order, ignoring host order */
93 src
= (*octetptr
) << 8;
95 /* declare second octet as least significant */
102 /* accumulate remaining octet */
103 src
= (*octetptr
) << 8;
106 /* add deferred carry bits */
107 acc
= (acc
>> 16) + (acc
& 0x0000ffffUL
);
108 if ((acc
& 0xffff0000UL
) != 0) {
109 acc
= (acc
>> 16) + (acc
& 0x0000ffffUL
);
111 /* This maybe a little confusing: reorder sum using htons()
112 instead of ntohs() since it has a little less call overhead.
113 The caller must invert bits for Internet sum ! */
114 return htons((u16_t
)acc
);
118 #if (LWIP_CHKSUM_ALGORITHM == 2) /* Alternative version #2 */
124 * IP checksum two bytes at a time with support for
126 * Works for len up to and including 0x20000.
127 * by Curt McDowell, Broadcom Corp. 12/08/2005
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)
135 lwip_standard_chksum(void *dataptr
, int len
)
137 u8_t
*pb
= (u8_t
*)dataptr
;
140 int odd
= ((mem_ptr_t
)pb
& 1);
142 /* Get aligned to u16_t */
143 if (odd
&& len
> 0) {
144 ((u8_t
*)&t
)[1] = *pb
++;
148 /* Add the bulk of the data */
149 ps
= (u16_t
*)(void *)pb
;
155 /* Consume left-over byte, if any */
157 ((u8_t
*)&t
)[0] = *(u8_t
*)ps
;
163 /* Fold 32-bit sum to 16 bits
164 calling this twice is propably faster than if statements... */
165 sum
= FOLD_U32T(sum
);
166 sum
= FOLD_U32T(sum
);
168 /* Swap if alignment was odd */
170 sum
= SWAP_BYTES_IN_WORD(sum
);
177 #if (LWIP_CHKSUM_ALGORITHM == 3) /* Alternative version #3 */
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.
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)
187 * by Curt McDowell, Broadcom Corp. December 8th, 2005
191 lwip_standard_chksum(void *dataptr
, int len
)
193 u8_t
*pb
= (u8_t
*)dataptr
;
197 /* starts at odd byte address? */
198 int odd
= ((mem_ptr_t
)pb
& 1);
200 if (odd
&& len
> 0) {
201 ((u8_t
*)&t
)[1] = *pb
++;
207 if (((mem_ptr_t
)ps
& 3) && len
> 1) {
215 tmp
= sum
+ *pl
++; /* ping */
217 tmp
++; /* add back carry */
220 sum
= tmp
+ *pl
++; /* pong */
222 sum
++; /* add back carry */
228 /* make room in upper bits */
229 sum
= FOLD_U32T(sum
);
233 /* 16-bit aligned word remaining? */
239 /* dangling tail byte remaining? */
240 if (len
> 0) { /* include odd byte */
241 ((u8_t
*)&t
)[0] = *(u8_t
*)ps
;
244 sum
+= t
; /* add end bytes */
246 /* Fold 32-bit sum to 16 bits
247 calling this twice is propably faster than if statements... */
248 sum
= FOLD_U32T(sum
);
249 sum
= FOLD_U32T(sum
);
252 sum
= SWAP_BYTES_IN_WORD(sum
);
259 /* inet_chksum_pseudo:
261 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
262 * IP addresses are expected to be in network byte order.
264 * @param p chain of pbufs over that a checksum should be calculated (ip data part)
265 * @param src source ip address (used for checksum of pseudo header)
266 * @param dst destination ip address (used for checksum of pseudo header)
267 * @param proto ip protocol (used for checksum of pseudo header)
268 * @param proto_len length of the ip data part (used for checksum of pseudo header)
269 * @return checksum (as u16_t) to be saved directly in the protocol header
272 inet_chksum_pseudo(struct pbuf
*p
,
273 ip_addr_t
*src
, ip_addr_t
*dest
,
274 u8_t proto
, u16_t proto_len
)
283 /* iterate through all pbuf in chain */
284 for(q
= p
; q
!= NULL
; q
= q
->next
) {
285 LWIP_DEBUGF(INET_DEBUG
, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
286 (void *)q
, (void *)q
->next
));
287 acc
+= LWIP_CHKSUM(q
->payload
, q
->len
);
288 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
289 /* just executing this next line is probably faster that the if statement needed
290 to check whether we really need to execute it, and does no harm */
291 acc
= FOLD_U32T(acc
);
292 if (q
->len
% 2 != 0) {
293 swapped
= 1 - swapped
;
294 acc
= SWAP_BYTES_IN_WORD(acc
);
296 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
300 acc
= SWAP_BYTES_IN_WORD(acc
);
302 addr
= ip4_addr_get_u32(src
);
303 acc
+= (addr
& 0xffffUL
);
304 acc
+= ((addr
>> 16) & 0xffffUL
);
305 addr
= ip4_addr_get_u32(dest
);
306 acc
+= (addr
& 0xffffUL
);
307 acc
+= ((addr
>> 16) & 0xffffUL
);
308 acc
+= (u32_t
)htons((u16_t
)proto
);
309 acc
+= (u32_t
)htons(proto_len
);
311 /* Fold 32-bit sum to 16 bits
312 calling this twice is propably faster than if statements... */
313 acc
= FOLD_U32T(acc
);
314 acc
= FOLD_U32T(acc
);
315 LWIP_DEBUGF(INET_DEBUG
, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F
"\n", acc
));
316 return (u16_t
)~(acc
& 0xffffUL
);
319 /* inet_chksum_pseudo:
321 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
322 * IP addresses are expected to be in network byte order.
324 * @param p chain of pbufs over that a checksum should be calculated (ip data part)
325 * @param src source ip address (used for checksum of pseudo header)
326 * @param dst destination ip address (used for checksum of pseudo header)
327 * @param proto ip protocol (used for checksum of pseudo header)
328 * @param proto_len length of the ip data part (used for checksum of pseudo header)
329 * @return checksum (as u16_t) to be saved directly in the protocol header
332 inet_chksum_pseudo_partial(struct pbuf
*p
,
333 ip_addr_t
*src
, ip_addr_t
*dest
,
334 u8_t proto
, u16_t proto_len
, u16_t chksum_len
)
344 /* iterate through all pbuf in chain */
345 for(q
= p
; (q
!= NULL
) && (chksum_len
> 0); q
= q
->next
) {
346 LWIP_DEBUGF(INET_DEBUG
, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
347 (void *)q
, (void *)q
->next
));
349 if (chklen
> chksum_len
) {
352 acc
+= LWIP_CHKSUM(q
->payload
, chklen
);
353 chksum_len
-= chklen
;
354 LWIP_ASSERT("delete me", chksum_len
< 0x7fff);
355 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
356 /* fold the upper bit down */
357 acc
= FOLD_U32T(acc
);
358 if (q
->len
% 2 != 0) {
359 swapped
= 1 - swapped
;
360 acc
= SWAP_BYTES_IN_WORD(acc
);
362 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
366 acc
= SWAP_BYTES_IN_WORD(acc
);
368 addr
= ip4_addr_get_u32(src
);
369 acc
+= (addr
& 0xffffUL
);
370 acc
+= ((addr
>> 16) & 0xffffUL
);
371 addr
= ip4_addr_get_u32(dest
);
372 acc
+= (addr
& 0xffffUL
);
373 acc
+= ((addr
>> 16) & 0xffffUL
);
374 acc
+= (u32_t
)htons((u16_t
)proto
);
375 acc
+= (u32_t
)htons(proto_len
);
377 /* Fold 32-bit sum to 16 bits
378 calling this twice is propably faster than if statements... */
379 acc
= FOLD_U32T(acc
);
380 acc
= FOLD_U32T(acc
);
381 LWIP_DEBUGF(INET_DEBUG
, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F
"\n", acc
));
382 return (u16_t
)~(acc
& 0xffffUL
);
387 * Calculates the Internet checksum over a portion of memory. Used primarily for IP
390 * @param dataptr start of the buffer to calculate the checksum (no alignment needed)
391 * @param len length of the buffer to calculate the checksum
392 * @return checksum (as u16_t) to be saved directly in the protocol header
396 inet_chksum(void *dataptr
, u16_t len
)
398 return ~LWIP_CHKSUM(dataptr
, len
);
402 * Calculate a checksum over a chain of pbufs (without pseudo-header, much like
403 * inet_chksum only pbufs are used).
405 * @param p pbuf chain over that the checksum should be calculated
406 * @return checksum (as u16_t) to be saved directly in the protocol header
409 inet_chksum_pbuf(struct pbuf
*p
)
417 for(q
= p
; q
!= NULL
; q
= q
->next
) {
418 acc
+= LWIP_CHKSUM(q
->payload
, q
->len
);
419 acc
= FOLD_U32T(acc
);
420 if (q
->len
% 2 != 0) {
421 swapped
= 1 - swapped
;
422 acc
= SWAP_BYTES_IN_WORD(acc
);
427 acc
= SWAP_BYTES_IN_WORD(acc
);
429 return (u16_t
)~(acc
& 0xffffUL
);
432 /* These are some implementations for LWIP_CHKSUM_COPY, which copies data
433 * like MEMCPY but generates a checksum at the same time. Since this is a
434 * performance-sensitive function, you might want to create your own version
435 * in assembly targeted at your hardware by defining it in lwipopts.h:
436 * #define LWIP_CHKSUM_COPY(dst, src, len) your_chksum_copy(dst, src, len)
439 #if (LWIP_CHKSUM_COPY_ALGORITHM == 1) /* Version #1 */
440 /** Safe but slow: first call MEMCPY, then call LWIP_CHKSUM.
441 * For architectures with big caches, data might still be in cache when
442 * generating the checksum after copying.
445 lwip_chksum_copy(void *dst
, const void *src
, u16_t len
)
447 MEMCPY(dst
, src
, len
);
448 return LWIP_CHKSUM(dst
, len
);
450 #endif /* (LWIP_CHKSUM_COPY_ALGORITHM == 1) */