127bfbee1SPeter Avalos /* in_cksum.c
227bfbee1SPeter Avalos * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
327bfbee1SPeter Avalos * pointers/lengths giving the pieces to be checksummed. Also using
427bfbee1SPeter Avalos * Tahoe/CGI version of ADDCARRY(x) macro instead of from portable version.
527bfbee1SPeter Avalos */
627bfbee1SPeter Avalos
727bfbee1SPeter Avalos /*
827bfbee1SPeter Avalos * Copyright (c) 1988, 1992, 1993
927bfbee1SPeter Avalos * The Regents of the University of California. All rights reserved.
1027bfbee1SPeter Avalos *
1127bfbee1SPeter Avalos * Redistribution and use in source and binary forms, with or without
1227bfbee1SPeter Avalos * modification, are permitted provided that the following conditions
1327bfbee1SPeter Avalos * are met:
1427bfbee1SPeter Avalos * 1. Redistributions of source code must retain the above copyright
1527bfbee1SPeter Avalos * notice, this list of conditions and the following disclaimer.
1627bfbee1SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1727bfbee1SPeter Avalos * notice, this list of conditions and the following disclaimer in the
1827bfbee1SPeter Avalos * documentation and/or other materials provided with the distribution.
1927bfbee1SPeter Avalos * 3. Neither the name of the University nor the names of its contributors
2027bfbee1SPeter Avalos * may be used to endorse or promote products derived from this software
2127bfbee1SPeter Avalos * without specific prior written permission.
2227bfbee1SPeter Avalos *
2327bfbee1SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2427bfbee1SPeter Avalos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2527bfbee1SPeter Avalos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2627bfbee1SPeter Avalos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2727bfbee1SPeter Avalos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2827bfbee1SPeter Avalos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2927bfbee1SPeter Avalos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3027bfbee1SPeter Avalos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3127bfbee1SPeter Avalos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3227bfbee1SPeter Avalos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3327bfbee1SPeter Avalos * SUCH DAMAGE.
3427bfbee1SPeter Avalos *
3527bfbee1SPeter Avalos * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
3627bfbee1SPeter Avalos */
3727bfbee1SPeter Avalos
3827bfbee1SPeter Avalos #ifdef HAVE_CONFIG_H
39*ed775ee7SAntonio Huete Jimenez # include <config.h>
4027bfbee1SPeter Avalos #endif
4127bfbee1SPeter Avalos
42*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
4327bfbee1SPeter Avalos
44411677aeSAaron LI #include "netdissect.h"
4527bfbee1SPeter Avalos
4627bfbee1SPeter Avalos /*
4727bfbee1SPeter Avalos * Checksum routine for Internet Protocol family headers (Portable Version).
4827bfbee1SPeter Avalos *
4927bfbee1SPeter Avalos * This routine is very heavily used in the network
5027bfbee1SPeter Avalos * code and should be modified for each CPU to be as fast as possible.
5127bfbee1SPeter Avalos */
5227bfbee1SPeter Avalos
5327bfbee1SPeter Avalos #define ADDCARRY(x) {if ((x) > 65535) (x) -= 65535;}
5427bfbee1SPeter Avalos #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
5527bfbee1SPeter Avalos
56411677aeSAaron LI uint16_t
in_cksum(const struct cksum_vec * vec,int veclen)5727bfbee1SPeter Avalos in_cksum(const struct cksum_vec *vec, int veclen)
5827bfbee1SPeter Avalos {
59*ed775ee7SAntonio Huete Jimenez const uint16_t *w;
60*ed775ee7SAntonio Huete Jimenez int sum = 0;
61*ed775ee7SAntonio Huete Jimenez int mlen = 0;
6227bfbee1SPeter Avalos int byte_swapped = 0;
6327bfbee1SPeter Avalos
6427bfbee1SPeter Avalos union {
65411677aeSAaron LI uint8_t c[2];
66411677aeSAaron LI uint16_t s;
6727bfbee1SPeter Avalos } s_util;
6827bfbee1SPeter Avalos union {
69411677aeSAaron LI uint16_t s[2];
70411677aeSAaron LI uint32_t l;
7127bfbee1SPeter Avalos } l_util;
7227bfbee1SPeter Avalos
7327bfbee1SPeter Avalos for (; veclen != 0; vec++, veclen--) {
7427bfbee1SPeter Avalos if (vec->len == 0)
7527bfbee1SPeter Avalos continue;
76411677aeSAaron LI w = (const uint16_t *)(const void *)vec->ptr;
7727bfbee1SPeter Avalos if (mlen == -1) {
7827bfbee1SPeter Avalos /*
7927bfbee1SPeter Avalos * The first byte of this chunk is the continuation
8027bfbee1SPeter Avalos * of a word spanning between this chunk and the
8127bfbee1SPeter Avalos * last chunk.
8227bfbee1SPeter Avalos *
8327bfbee1SPeter Avalos * s_util.c[0] is already saved when scanning previous
8427bfbee1SPeter Avalos * chunk.
8527bfbee1SPeter Avalos */
86411677aeSAaron LI s_util.c[1] = *(const uint8_t *)w;
8727bfbee1SPeter Avalos sum += s_util.s;
88411677aeSAaron LI w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
8927bfbee1SPeter Avalos mlen = vec->len - 1;
9027bfbee1SPeter Avalos } else
9127bfbee1SPeter Avalos mlen = vec->len;
9227bfbee1SPeter Avalos /*
9327bfbee1SPeter Avalos * Force to even boundary.
9427bfbee1SPeter Avalos */
95411677aeSAaron LI if ((1 & (uintptr_t) w) && (mlen > 0)) {
9627bfbee1SPeter Avalos REDUCE;
9727bfbee1SPeter Avalos sum <<= 8;
98411677aeSAaron LI s_util.c[0] = *(const uint8_t *)w;
99411677aeSAaron LI w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
10027bfbee1SPeter Avalos mlen--;
10127bfbee1SPeter Avalos byte_swapped = 1;
10227bfbee1SPeter Avalos }
10327bfbee1SPeter Avalos /*
10427bfbee1SPeter Avalos * Unroll the loop to make overhead from
10527bfbee1SPeter Avalos * branches &c small.
10627bfbee1SPeter Avalos */
10727bfbee1SPeter Avalos while ((mlen -= 32) >= 0) {
10827bfbee1SPeter Avalos sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
10927bfbee1SPeter Avalos sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
11027bfbee1SPeter Avalos sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
11127bfbee1SPeter Avalos sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
11227bfbee1SPeter Avalos w += 16;
11327bfbee1SPeter Avalos }
11427bfbee1SPeter Avalos mlen += 32;
11527bfbee1SPeter Avalos while ((mlen -= 8) >= 0) {
11627bfbee1SPeter Avalos sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
11727bfbee1SPeter Avalos w += 4;
11827bfbee1SPeter Avalos }
11927bfbee1SPeter Avalos mlen += 8;
12027bfbee1SPeter Avalos if (mlen == 0 && byte_swapped == 0)
12127bfbee1SPeter Avalos continue;
12227bfbee1SPeter Avalos REDUCE;
12327bfbee1SPeter Avalos while ((mlen -= 2) >= 0) {
12427bfbee1SPeter Avalos sum += *w++;
12527bfbee1SPeter Avalos }
12627bfbee1SPeter Avalos if (byte_swapped) {
12727bfbee1SPeter Avalos REDUCE;
12827bfbee1SPeter Avalos sum <<= 8;
12927bfbee1SPeter Avalos byte_swapped = 0;
13027bfbee1SPeter Avalos if (mlen == -1) {
131411677aeSAaron LI s_util.c[1] = *(const uint8_t *)w;
13227bfbee1SPeter Avalos sum += s_util.s;
13327bfbee1SPeter Avalos mlen = 0;
13427bfbee1SPeter Avalos } else
13527bfbee1SPeter Avalos mlen = -1;
13627bfbee1SPeter Avalos } else if (mlen == -1)
137411677aeSAaron LI s_util.c[0] = *(const uint8_t *)w;
13827bfbee1SPeter Avalos }
13927bfbee1SPeter Avalos if (mlen == -1) {
14027bfbee1SPeter Avalos /* The last mbuf has odd # of bytes. Follow the
14127bfbee1SPeter Avalos standard (the odd byte may be shifted left by 8 bits
14227bfbee1SPeter Avalos or not as determined by endian-ness of the machine) */
14327bfbee1SPeter Avalos s_util.c[1] = 0;
14427bfbee1SPeter Avalos sum += s_util.s;
14527bfbee1SPeter Avalos }
14627bfbee1SPeter Avalos REDUCE;
14727bfbee1SPeter Avalos return (~sum & 0xffff);
14827bfbee1SPeter Avalos }
14927bfbee1SPeter Avalos
15027bfbee1SPeter Avalos /*
15127bfbee1SPeter Avalos * Given the host-byte-order value of the checksum field in a packet
15227bfbee1SPeter Avalos * header, and the network-byte-order computed checksum of the data
15327bfbee1SPeter Avalos * that the checksum covers (including the checksum itself), compute
15427bfbee1SPeter Avalos * what the checksum field *should* have been.
15527bfbee1SPeter Avalos */
156411677aeSAaron LI uint16_t
in_cksum_shouldbe(uint16_t sum,uint16_t computed_sum)157411677aeSAaron LI in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
15827bfbee1SPeter Avalos {
159411677aeSAaron LI uint32_t shouldbe;
16027bfbee1SPeter Avalos
16127bfbee1SPeter Avalos /*
16227bfbee1SPeter Avalos * The value that should have gone into the checksum field
16327bfbee1SPeter Avalos * is the negative of the value gotten by summing up everything
16427bfbee1SPeter Avalos * *but* the checksum field.
16527bfbee1SPeter Avalos *
16627bfbee1SPeter Avalos * We can compute that by subtracting the value of the checksum
16727bfbee1SPeter Avalos * field from the sum of all the data in the packet, and then
16827bfbee1SPeter Avalos * computing the negative of that value.
16927bfbee1SPeter Avalos *
17027bfbee1SPeter Avalos * "sum" is the value of the checksum field, and "computed_sum"
17127bfbee1SPeter Avalos * is the negative of the sum of all the data in the packets,
17227bfbee1SPeter Avalos * so that's -(-computed_sum - sum), or (sum + computed_sum).
17327bfbee1SPeter Avalos *
17427bfbee1SPeter Avalos * All the arithmetic in question is one's complement, so the
17527bfbee1SPeter Avalos * addition must include an end-around carry; we do this by
17627bfbee1SPeter Avalos * doing the arithmetic in 32 bits (with no sign-extension),
17727bfbee1SPeter Avalos * and then adding the upper 16 bits of the sum, which contain
17827bfbee1SPeter Avalos * the carry, to the lower 16 bits of the sum, and then do it
17927bfbee1SPeter Avalos * again in case *that* sum produced a carry.
18027bfbee1SPeter Avalos *
18127bfbee1SPeter Avalos * As RFC 1071 notes, the checksum can be computed without
18227bfbee1SPeter Avalos * byte-swapping the 16-bit words; summing 16-bit words
18327bfbee1SPeter Avalos * on a big-endian machine gives a big-endian checksum, which
18427bfbee1SPeter Avalos * can be directly stuffed into the big-endian checksum fields
18527bfbee1SPeter Avalos * in protocol headers, and summing words on a little-endian
18627bfbee1SPeter Avalos * machine gives a little-endian checksum, which must be
18727bfbee1SPeter Avalos * byte-swapped before being stuffed into a big-endian checksum
18827bfbee1SPeter Avalos * field.
18927bfbee1SPeter Avalos *
19027bfbee1SPeter Avalos * "computed_sum" is a network-byte-order value, so we must put
19127bfbee1SPeter Avalos * it in host byte order before subtracting it from the
19227bfbee1SPeter Avalos * host-byte-order value from the header; the adjusted checksum
19327bfbee1SPeter Avalos * will be in host byte order, which is what we'll return.
19427bfbee1SPeter Avalos */
19527bfbee1SPeter Avalos shouldbe = sum;
19627bfbee1SPeter Avalos shouldbe += ntohs(computed_sum);
19727bfbee1SPeter Avalos shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
19827bfbee1SPeter Avalos shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
199*ed775ee7SAntonio Huete Jimenez return (uint16_t)shouldbe;
20027bfbee1SPeter Avalos }
201