blob: 83f39bcea18beb914564c6e69ab8ec42f64908cd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/* Taken from Etherboot */
#include "libopenbios/ipchecksum.h"
unsigned short ipchksum(const void *data, unsigned long length)
{
unsigned long sum;
unsigned long i;
const unsigned char *ptr;
union {
unsigned char byte[2];
unsigned short word;
} u;
/* In the most straight forward way possible,
* compute an ip style checksum.
*/
sum = 0;
ptr = data;
for(i = 0; i < length; i++) {
unsigned long value;
value = ptr[i];
if (i & 1) {
value <<= 8;
}
/* Add the new value */
sum += value;
/* Wrap around the carry */
if (sum > 0xFFFF) {
sum = (sum + (sum >> 16)) & 0xFFFF;
}
}
u.byte[0] = (unsigned char) sum;
u.byte[1] = (unsigned char) (sum >> 8);
return (unsigned short) ~u.word;
}
unsigned short add_ipchksums(unsigned long offset, unsigned short sum, unsigned short new)
{
unsigned long checksum;
sum = ~sum & 0xFFFF;
new = ~new & 0xFFFF;
if (offset & 1) {
/* byte swap the sum if it came from an odd offset
* since the computation is endian independant this
* works.
*/
new = (new << 8) | (new >> 8);
}
checksum = sum + new;
if (checksum > 0xFFFF) {
checksum -= 0xFFFF;
}
return (~checksum) & 0xFFFF;
}
|