1 /**
2  * @file
3  * IP checksum calculation functions
4  */
5 
6 /*
7  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Adam Dunkels <adam@sics.se>
35  *
36  */
37 #ifndef LWIP_HDR_INET_CHKSUM_H
38 #define LWIP_HDR_INET_CHKSUM_H
39 
40 #include "lwip/opt.h"
41 
42 #include "lwip/pbuf.h"
43 #include "lwip/ip_addr.h"
44 
45 /** Swap the bytes in an u16_t: much like lwip_htons() for little-endian */
46 #ifndef SWAP_BYTES_IN_WORD
47 #define SWAP_BYTES_IN_WORD(w) (((w) & 0xff) << 8) | (((w) & 0xff00) >> 8)
48 #endif /* SWAP_BYTES_IN_WORD */
49 
50 /** Split an u32_t in two u16_ts and add them up */
51 #ifndef FOLD_U32T
52 #define FOLD_U32T(u)          ((u32_t)(((u) >> 16) + ((u) & 0x0000ffffUL)))
53 #endif
54 
55 #if LWIP_CHECKSUM_ON_COPY
56 /** Function-like macro: same as MEMCPY but returns the checksum of copied data
57     as u16_t */
58 # ifndef LWIP_CHKSUM_COPY
59 #  define LWIP_CHKSUM_COPY(dst, src, len) lwip_chksum_copy(dst, src, len)
60 #  ifndef LWIP_CHKSUM_COPY_ALGORITHM
61 #   define LWIP_CHKSUM_COPY_ALGORITHM 1
62 #  endif /* LWIP_CHKSUM_COPY_ALGORITHM */
63 # else /* LWIP_CHKSUM_COPY */
64 #  define LWIP_CHKSUM_COPY_ALGORITHM 0
65 # endif /* LWIP_CHKSUM_COPY */
66 #else /* LWIP_CHECKSUM_ON_COPY */
67 # define LWIP_CHKSUM_COPY_ALGORITHM 0
68 #endif /* LWIP_CHECKSUM_ON_COPY */
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 u16_t inet_chksum(const void *dataptr, u16_t len);
75 u16_t inet_chksum_pbuf(struct pbuf *p);
76 #if LWIP_CHKSUM_COPY_ALGORITHM
77 u16_t lwip_chksum_copy(void *dst, const void *src, u16_t len);
78 #endif /* LWIP_CHKSUM_COPY_ALGORITHM */
79 
80 #if LWIP_IPV4
81 u16_t inet_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
82        const ip4_addr_t *src, const ip4_addr_t *dest);
83 u16_t inet_chksum_pseudo_partial(struct pbuf *p, u8_t proto,
84        u16_t proto_len, u16_t chksum_len, const ip4_addr_t *src, const ip4_addr_t *dest);
85 #endif /* LWIP_IPV4 */
86 
87 #if LWIP_IPV6
88 u16_t ip6_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
89        const ip6_addr_t *src, const ip6_addr_t *dest);
90 u16_t ip6_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len,
91        u16_t chksum_len, const ip6_addr_t *src, const ip6_addr_t *dest);
92 #endif /* LWIP_IPV6 */
93 
94 
95 u16_t ip_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
96        const ip_addr_t *src, const ip_addr_t *dest);
97 u16_t ip_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len,
98        u16_t chksum_len, const ip_addr_t *src, const ip_addr_t *dest);
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #endif /* LWIP_HDR_INET_H */
105