2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
36 * Functions common to all TCP/IP modules, such as the Internet checksum and the
37 * byte order functions.
44 #include "lwip/arch.h"
47 #include "lwip/inet.h"
51 /* This is a reference implementation of the checksum algorithm, with the
52 * aim of being simple, correct and fully portable. Checksumming is the
53 * first thing you would want to optimize for your platform. You will
54 * need to port it to your architecture and in your sys_arch.h:
56 * #define LWIP_CHKSUM <your_checksum_routine>
59 #define LWIP_CHKSUM lwip_standard_chksum
64 * @param dataptr points to start of data to be summed at any boundary
65 * @param len length of data to be summed
66 * @return host order (!) lwip checksum (non-inverted Internet sum)
68 * @note accumulator size limits summable lenght to 64k
69 * @note host endianess is irrelevant (p3 RFC1071)
72 lwip_standard_chksum(void *dataptr
, u16_t len
)
79 /* dataptr may be at odd or even addresses */
80 octetptr
= (u8_t
*)dataptr
;
83 /* declare first octet as most significant
84 thus assume network order, ignoring host order */
85 src
= (*octetptr
) << 8;
87 /* declare second octet as least significant */
95 /* accumulate remaining octet */
96 src
= (*octetptr
) << 8;
99 /* add deferred carry bits */
100 acc
= (acc
>> 16) + (acc
& 0x0000ffffUL
);
101 if ((acc
& 0xffff0000) != 0) {
102 acc
= (acc
>> 16) + (acc
& 0x0000ffffUL
);
104 /* This maybe a little confusing: reorder sum using htons()
105 instead of ntohs() since it has a little less call overhead.
106 The caller must invert bits for Internet sum ! */
107 return htons((u16_t
)acc
);
118 * IP checksum two bytes at a time with support for
120 * Works for len up to and including 0x20000.
121 * by Curt McDowell, Broadcom Corp. 12/08/2005
125 lwip_standard_chksum2(void *dataptr
, int len
)
130 int odd
= ((u32_t
)pb
& 1);
132 /* Get aligned to u16_t */
133 if (odd
&& len
> 0) {
134 ((u8_t
*)&t
)[1] = *pb
++;
138 /* Add the bulk of the data */
145 /* Consume left-over byte, if any */
147 ((u8_t
*)&t
)[0] = *(u8_t
*)ps
;;
152 /* Fold 32-bit sum to 16 bits */
154 sum
= (sum
& 0xffff) + (sum
>> 16);
156 /* Swap if alignment was odd */
158 sum
= ((sum
& 0xff) << 8) | ((sum
& 0xff00) >> 8);
164 * An optimized checksum routine. Basically, it uses loop-unrolling on
165 * the checksum loop, treating the head and tail bytes specially, whereas
166 * the inner loop acts on 8 bytes at a time.
168 * @arg start of buffer to be checksummed. May be an odd byte address.
169 * @len number of bytes in the buffer to be checksummed.
171 * @todo First argument type conflicts with generic checksum routine.
173 * by Curt McDowell, Broadcom Corp. December 8th, 2005
177 lwip_standard_chksum4(u8_t
*pb
, int len
)
182 /* starts at odd byte address? */
183 int odd
= ((u32_t
)pb
& 1);
185 if (odd
&& len
> 0) {
186 ((u8_t
*)&t
)[1] = *pb
++;
192 if (((u32_t
)ps
& 3) && len
> 1) {
200 tmp
= sum
+ *pl
++; /* ping */
202 tmp
++; /* add back carry */
204 sum
= tmp
+ *pl
++; /* pong */
206 sum
++; /* add back carry */
211 /* make room in upper bits */
212 sum
= (sum
>> 16) + (sum
& 0xffff);
216 /* 16-bit aligned word remaining? */
222 /* dangling tail byte remaining? */
223 if (len
> 0) /* include odd byte */
224 ((u8_t
*)&t
)[0] = *(u8_t
*)ps
;
226 sum
+= t
; /* add end bytes */
228 while (sum
>> 16) /* combine halves */
229 sum
= (sum
>> 16) + (sum
& 0xffff);
232 sum
= ((sum
& 0xff) << 8) | ((sum
& 0xff00) >> 8);
238 /* inet_chksum_pseudo:
240 * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
244 inet_chksum_pseudo(struct pbuf
*p
,
245 struct ip_addr
*src
, struct ip_addr
*dest
,
246 u8_t proto
, u16_t proto_len
)
254 /* iterate through all pbuf in chain */
255 for(q
= p
; q
!= NULL
; q
= q
->next
) {
256 LWIP_DEBUGF(INET_DEBUG
, ("inet_chksum_pseudo(): checksumming pbuf %p (has next %p) \n",
257 (void *)q
, (void *)q
->next
));
258 acc
+= LWIP_CHKSUM(q
->payload
, q
->len
);
259 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): unwrapped lwip_chksum()=%"X32_F" \n", acc));*/
261 acc
= (acc
& 0xffffUL
) + (acc
>> 16);
263 if (q
->len
% 2 != 0) {
264 swapped
= 1 - swapped
;
265 acc
= ((acc
& 0xff) << 8) | ((acc
& 0xff00UL
) >> 8);
267 /*LWIP_DEBUGF(INET_DEBUG, ("inet_chksum_pseudo(): wrapped lwip_chksum()=%"X32_F" \n", acc));*/
271 acc
= ((acc
& 0xff) << 8) | ((acc
& 0xff00UL
) >> 8);
273 acc
+= (src
->addr
& 0xffffUL
);
274 acc
+= ((src
->addr
>> 16) & 0xffffUL
);
275 acc
+= (dest
->addr
& 0xffffUL
);
276 acc
+= ((dest
->addr
>> 16) & 0xffffUL
);
277 acc
+= (u32_t
)htons((u16_t
)proto
);
278 acc
+= (u32_t
)htons(proto_len
);
281 acc
= (acc
& 0xffffUL
) + (acc
>> 16);
283 LWIP_DEBUGF(INET_DEBUG
, ("inet_chksum_pseudo(): pbuf chain lwip_chksum()=%"X32_F
"\n", acc
));
284 return (u16_t
)~(acc
& 0xffffUL
);
289 * Calculates the Internet checksum over a portion of memory. Used primarely for IP
294 inet_chksum(void *dataptr
, u16_t len
)
298 acc
= LWIP_CHKSUM(dataptr
, len
);
300 acc
= (acc
& 0xffff) + (acc
>> 16);
302 return (u16_t
)~(acc
& 0xffff);
306 inet_chksum_pbuf(struct pbuf
*p
)
314 for(q
= p
; q
!= NULL
; q
= q
->next
) {
315 acc
+= LWIP_CHKSUM(q
->payload
, q
->len
);
317 acc
= (acc
& 0xffffUL
) + (acc
>> 16);
319 if (q
->len
% 2 != 0) {
320 swapped
= 1 - swapped
;
321 acc
= (acc
& 0x00ffUL
<< 8) | (acc
& 0xff00UL
>> 8);
326 acc
= ((acc
& 0x00ffUL
) << 8) | ((acc
& 0xff00UL
) >> 8);
328 return (u16_t
)~(acc
& 0xffffUL
);