|
|
1.1 root 1: /*
2: * Copyright (c) 1988 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: *
17: * @(#)in_cksum.c 7.2 (Berkeley) 6/29/88
18: */
19:
20: #include "../h/types.h"
21: #include "../h/mbuf.h"
22:
23: /*
24: * Checksum routine for Internet Protocol family headers (Portable Version).
25: *
26: * This routine is very heavily used in the network
27: * code and should be modified for each CPU to be as fast as possible.
28: */
29:
30: #define ADDCARRY(x) (x > 65535 ? x -= 65535 : x)
31: #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
32:
33: in_cksum_c(m, len)
34: register struct mbuf *m;
35: register int len;
36: {
37: register u_short *w;
38: register int sum = 0;
39: register int mlen = 0;
40: int byte_swapped = 0;
41:
42: union {
43: char c[2];
44: u_short s;
45: } s_util;
46: union {
47: u_short s[2];
48: long l;
49: } l_util;
50:
51: for (;m && len; m = m->m_next) {
52: if (m->m_len == 0)
53: continue;
54: w = mtod(m, u_short *);
55: if (mlen == -1) {
56: /*
57: * The first byte of this mbuf is the continuation
58: * of a word spanning between this mbuf and the
59: * last mbuf.
60: *
61: * s_util.c[0] is already saved when scanning previous
62: * mbuf.
63: */
64: s_util.c[1] = *(char *)w;
65: sum += s_util.s;
66: w = (u_short *)((char *)w + 1);
67: mlen = m->m_len - 1;
68: len--;
69: } else
70: mlen = m->m_len;
71: if (len < mlen)
72: mlen = len;
73: len -= mlen;
74: /*
75: * Force to even boundary.
76: */
77: if ((1 & (int) w) && (mlen > 0)) {
78: REDUCE;
79: sum <<= 8;
80: s_util.c[0] = *(u_char *)w;
81: w = (u_short *)((char *)w + 1);
82: mlen--;
83: byte_swapped = 1;
84: }
85: /*
86: * Unroll the loop to make overhead from
87: * branches &c small.
88: */
89: while ((mlen -= 32) >= 0) {
90: sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
91: sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
92: sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
93: sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
94: w += 16;
95: }
96: mlen += 32;
97: while ((mlen -= 8) >= 0) {
98: sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
99: w += 4;
100: }
101: mlen += 8;
102: if (mlen == 0 && byte_swapped == 0)
103: continue;
104: REDUCE;
105: while ((mlen -= 2) >= 0) {
106: sum += *w++;
107: }
108: if (byte_swapped) {
109: REDUCE;
110: sum <<= 8;
111: byte_swapped = 0;
112: if (mlen == -1) {
113: s_util.c[1] = *(char *)w;
114: sum += s_util.s;
115: mlen = 0;
116: } else
117: mlen = -1;
118: } else if (mlen == -1)
119: s_util.c[0] = *(char *)w;
120: }
121: if (len)
122: printf("cksum: out of data\n");
123: if (mlen == -1) {
124: /* The last mbuf has odd # of bytes. Follow the
125: standard (the odd byte may be shifted left by 8 bits
126: or not as determined by endian-ness of the machine) */
127: s_util.c[1] = 0;
128: sum += s_util.s;
129: }
130: REDUCE;
131: return (~sum & 0xffff);
132: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.