• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) ((u16_t)(((w) & 0xff) << 8) | (u16_t)(((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 #if defined (__cplusplus) && __cplusplus
71 extern "C" {
72 #endif
73 
74 #if (LWIP_CHKSUM_ALGORITHM == 4) || (LWIP_CHKSUM_COPY_ALGORITHM == 2)
75 /* @brief Check and reverse the data.
76  *
77  * @par Description:
78  * This API is used to check and reverse the data. The function is
79  * used for converting 32-bits accumulation into 16-bits checksum.
80  *
81  * @attention
82  * The checksum calculation can be divided into two steps: 1.accumulation; 2.reverse.
83  * This function is used to complate the second step. The input value get from the first step.
84  *
85  * @param sum [IN] the value of sum for the data which is needed to check and reverse.
86  *
87  * @retval uint16_t value of checksum.
88  */
csum_fold(unsigned int sum)89 static inline unsigned short csum_fold(unsigned int sum)
90 {
91 #ifdef ARCH_ARM
92     __asm__(
93     "add    %0, %1, %1, ror #16    @ csum_fold"
94     : "=r" (sum)
95     : "r" (sum)
96     : "cc");
97     return (unsigned short)(~(unsigned int)sum >> 16);
98 #else
99     sum = (sum & 0xffff) + (sum >> 16);
100     sum = (sum & 0xffff) + (sum >> 16);
101     return (unsigned short)(~sum);
102 #endif
103 }
104 #endif /* (LWIP_CHKSUM_ALGORITHM == 4) || (LWIP_CHKSUM_COPY_ALGORITHM == 2) */
105 
106 u16_t inet_chksum(const void *dataptr, u16_t len);
107 u16_t inet_chksum_pbuf(struct pbuf *p);
108 #if LWIP_CHKSUM_COPY_ALGORITHM
109 u16_t lwip_chksum_copy(void *dst, const void *src, u16_t len);
110 #endif /* LWIP_CHKSUM_COPY_ALGORITHM */
111 
112 #if LWIP_IPV4
113 u16_t inet_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
114                          const ip4_addr_t *src, const ip4_addr_t *dest);
115 u16_t inet_chksum_pseudo_partial(struct pbuf *p, u8_t proto,
116                                  u16_t proto_len, u16_t chksum_len, const ip4_addr_t *src, const ip4_addr_t *dest);
117 #endif /* LWIP_IPV4 */
118 
119 #if LWIP_IPV6
120 u16_t ip6_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
121                         const ip6_addr_t *src, const ip6_addr_t *dest);
122 u16_t ip6_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len,
123                                 u16_t chksum_len, const ip6_addr_t *src, const ip6_addr_t *dest);
124 #endif /* LWIP_IPV6 */
125 
126 
127 u16_t ip_chksum_pseudo(struct pbuf *p, u8_t proto, u16_t proto_len,
128                        const ip_addr_t *src, const ip_addr_t *dest);
129 u16_t ip_chksum_pseudo_partial(struct pbuf *p, u8_t proto, u16_t proto_len,
130                                u16_t chksum_len, const ip_addr_t *src, const ip_addr_t *dest);
131 
132 #if defined (__cplusplus) && __cplusplus
133 }
134 #endif
135 
136 #endif /* LWIP_HDR_INET_H */
137 
138