change gdium conf to print message out both on uart and lcd
[pmon-gdium.git] / sys / netinet / in_cksum.c
blobd4855172ce01473a81bcc8914e060cedc6e4c4c5
1 /* $OpenBSD: in_cksum.c,v 1.3 1997/02/24 14:06:35 niklas Exp $ */
2 /* $NetBSD: in_cksum.c,v 1.11 1996/04/08 19:55:37 jonathan Exp $ */
4 /*
5 * Copyright (c) 1988, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
36 * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
39 #include <sys/param.h>
40 #include <sys/mbuf.h>
41 #include <sys/systm.h>
42 #include <netinet/in.h>
45 * Checksum routine for Internet Protocol family headers (Portable Version).
47 * This routine is very heavily used in the network
48 * code and should be modified for each CPU to be as fast as possible.
51 #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
52 #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
54 int
55 in_cksum(m, len)
56 register struct mbuf *m;
57 register int len;
59 register u_int16_t *w;
60 register int sum = 0;
61 register int mlen = 0;
62 int byte_swapped = 0;
64 union {
65 u_int8_t c[2];
66 u_int16_t s;
67 } s_util;
68 union {
69 u_int16_t s[2];
70 u_int32_t l;
71 } l_util;
73 for (;m && len; m = m->m_next) {
74 if (m->m_len == 0)
75 continue;
76 w = mtod(m, u_int16_t *);
77 if (mlen == -1) {
79 * The first byte of this mbuf is the continuation
80 * of a word spanning between this mbuf and the
81 * last mbuf.
83 * s_util.c[0] is already saved when scanning previous
84 * mbuf.
86 s_util.c[1] = *(u_int8_t *)w;
87 sum += s_util.s;
88 w = (u_int16_t *)((u_int8_t *)w + 1);
89 mlen = m->m_len - 1;
90 len--;
91 } else
92 mlen = m->m_len;
93 if (len < mlen)
94 mlen = len;
95 len -= mlen;
97 * Force to even boundary.
99 if ((1 & (long) w) && (mlen > 0)) {
100 REDUCE;
101 sum <<= 8;
102 s_util.c[0] = *(u_int8_t *)w;
103 w = (u_int16_t *)((int8_t *)w + 1);
104 mlen--;
105 byte_swapped = 1;
108 * Unroll the loop to make overhead from
109 * branches &c small.
111 while ((mlen -= 32) >= 0) {
112 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
113 sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
114 sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
115 sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
116 w += 16;
118 mlen += 32;
119 while ((mlen -= 8) >= 0) {
120 sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
121 w += 4;
123 mlen += 8;
124 if (mlen == 0 && byte_swapped == 0)
125 continue;
126 REDUCE;
127 while ((mlen -= 2) >= 0) {
128 sum += *w++;
130 if (byte_swapped) {
131 REDUCE;
132 sum <<= 8;
133 byte_swapped = 0;
134 if (mlen == -1) {
135 s_util.c[1] = *(u_int8_t *)w;
136 sum += s_util.s;
137 mlen = 0;
138 } else
139 mlen = -1;
140 } else if (mlen == -1)
141 s_util.c[0] = *(u_int8_t *)w;
143 if (len)
144 printf("cksum: out of data\n");
145 if (mlen == -1) {
146 /* The last mbuf has odd # of bytes. Follow the
147 standard (the odd byte may be shifted left by 8 bits
148 or not as determined by endian-ness of the machine) */
149 s_util.c[1] = 0;
150 sum += s_util.s;
152 REDUCE;
153 return (~sum & 0xffff);