radiotap: Updates to the radiotap dissector to avoid confusion.
[metze/wireshark/wip.git] / epan / in_cksum.c
1 /* in_cksum.c
2  * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
3  * pointers/lengths giving the pieces to be checksummed.  Also using
4  * Tahoe/CGI version of ADDCARRY(x) macro instead of from portable version.
5  *
6  * Copyright (c) 1988, 1992, 1993
7  *      The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)in_cksum.c  8.1 (Berkeley) 6/10/93
34  */
35
36 #include "config.h"
37
38 #include <glib.h>
39
40 #include <epan/tvbuff.h>
41 #include <epan/in_cksum.h>
42
43 /*
44  * Checksum routine for Internet Protocol family headers (Portable Version).
45  *
46  * This routine is very heavily used in the network
47  * code and should be modified for each CPU to be as fast as possible.
48  */
49
50 #define ADDCARRY(x)  {if ((x) > 65535) (x) -= 65535;}
51 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
52
53 int
54 in_cksum(const vec_t *vec, int veclen)
55 {
56         register const guint16 *w;
57         register int sum = 0;
58         register int mlen = 0;
59         int byte_swapped = 0;
60
61         union {
62                 guint8  c[2];
63                 guint16 s;
64         } s_util;
65         union {
66                 guint16 s[2];
67                 guint32 l;
68         } l_util;
69
70         for (; veclen != 0; vec++, veclen--) {
71                 if (vec->len == 0)
72                         continue;
73                 w = (const guint16 *)(const void *)vec->ptr;
74                 if (mlen == -1) {
75                         /*
76                          * The first byte of this chunk is the continuation
77                          * of a word spanning between this chunk and the
78                          * last chunk.
79                          *
80                          * s_util.c[0] is already saved when scanning previous
81                          * chunk.
82                          */
83                         s_util.c[1] = *(const guint8 *)w;
84                         sum += s_util.s;
85                         w = (const guint16 *)(const void *)((const guint8 *)w + 1);
86                         mlen = vec->len - 1;
87                 } else
88                         mlen = vec->len;
89                 /*
90                  * Force to even boundary.
91                  */
92                 if ((1 & (gintptr)w) && (mlen > 0)) {
93                         REDUCE;
94                         sum <<= 8;
95                         s_util.c[0] = *(const guint8 *)w;
96                         w = (const guint16 *)(const void *)((const guint8 *)w + 1);
97                         mlen--;
98                         byte_swapped = 1;
99                 }
100                 /*
101                  * Unroll the loop to make overhead from
102                  * branches &c small.
103                  */
104                 while ((mlen -= 32) >= 0) {
105                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
106                         sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
107                         sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
108                         sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
109                         w += 16;
110                 }
111                 mlen += 32;
112                 while ((mlen -= 8) >= 0) {
113                         sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
114                         w += 4;
115                 }
116                 mlen += 8;
117                 if (mlen == 0 && byte_swapped == 0)
118                         continue;
119                 REDUCE;
120                 while ((mlen -= 2) >= 0) {
121                         sum += *w++;
122                 }
123                 if (byte_swapped) {
124                         REDUCE;
125                         sum <<= 8;
126                         byte_swapped = 0;
127                         if (mlen == -1) {
128                                 s_util.c[1] = *(const guint8 *)w;
129                                 sum += s_util.s;
130                                 mlen = 0;
131                         } else
132                                 mlen = -1;
133                 } else if (mlen == -1)
134                         s_util.c[0] = *(const guint8 *)w;
135         }
136         if (mlen == -1) {
137                 /* The last mbuf has odd # of bytes. Follow the
138                    standard (the odd byte may be shifted left by 8 bits
139                    or not as determined by endian-ness of the machine) */
140                 s_util.c[1] = 0;
141                 sum += s_util.s;
142         }
143         REDUCE;
144         return (~sum & 0xffff);
145 }
146
147 guint16
148 ip_checksum(const guint8 *ptr, int len)
149 {
150         vec_t cksum_vec[1];
151
152         SET_CKSUM_VEC_PTR(cksum_vec[0], ptr, len);
153         return in_cksum(&cksum_vec[0], 1);
154 }
155
156 guint16
157 ip_checksum_tvb(tvbuff_t *tvb, int offset, int len)
158 {
159         vec_t cksum_vec[1];
160
161         SET_CKSUM_VEC_TVB(cksum_vec[0], tvb, offset, len);
162         return in_cksum(&cksum_vec[0], 1);
163 }
164
165 /*
166  * Given the host-byte-order value of the checksum field in a packet
167  * header, and the network-byte-order computed checksum of the data
168  * that the checksum covers (including the checksum itself), compute
169  * what the checksum field *should* have been.
170  */
171 guint16
172 in_cksum_shouldbe(guint16 sum, guint16 computed_sum)
173 {
174         guint32 shouldbe;
175
176         /*
177          * The value that should have gone into the checksum field
178          * is the negative of the value gotten by summing up everything
179          * *but* the checksum field.
180          *
181          * We can compute that by subtracting the value of the checksum
182          * field from the sum of all the data in the packet, and then
183          * computing the negative of that value.
184          *
185          * "sum" is the value of the checksum field, and "computed_sum"
186          * is the negative of the sum of all the data in the packets,
187          * so that's -(-computed_sum - sum), or (sum + computed_sum).
188          *
189          * All the arithmetic in question is one's complement, so the
190          * addition must include an end-around carry; we do this by
191          * doing the arithmetic in 32 bits (with no sign-extension),
192          * and then adding the upper 16 bits of the sum, which contain
193          * the carry, to the lower 16 bits of the sum, and then do it
194          * again in case *that* sum produced a carry.
195          *
196          * As RFC 1071 notes, the checksum can be computed without
197          * byte-swapping the 16-bit words; summing 16-bit words
198          * on a big-endian machine gives a big-endian checksum, which
199          * can be directly stuffed into the big-endian checksum fields
200          * in protocol headers, and summing words on a little-endian
201          * machine gives a little-endian checksum, which must be
202          * byte-swapped before being stuffed into a big-endian checksum
203          * field.
204          *
205          * "computed_sum" is a network-byte-order value, so we must put
206          * it in host byte order before subtracting it from the
207          * host-byte-order value from the header; the adjusted checksum
208          * will be in host byte order, which is what we'll return.
209          */
210         shouldbe = sum;
211         shouldbe += g_ntohs(computed_sum);
212         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
213         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
214         return shouldbe;
215 }
216
217 /*
218  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
219  *
220  * Local variables:
221  * c-basic-offset: 8
222  * tab-width: 8
223  * indent-tabs-mode: t
224  * End:
225  *
226  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
227  * :indentSize=8:tabSize=8:noTabs=false:
228  */