4 #include <netinet/in.h>
5 #include <netinet/ip.h>
9 static inline unsigned short csum(unsigned short *buf
, int nwords
)
13 for (sum
= 0; nwords
> 0; nwords
--)
15 sum
= (sum
>> 16) + (sum
& 0xffff);
21 static inline uint16_t calc_csum(void *addr
, size_t len
,
22 int ccsum __maybe_unused
)
24 return csum(addr
, len
>> 1);
27 static inline uint16_t csum_expected(uint16_t sum
, uint16_t computed_sum
)
32 shouldbe
+= ntohs(computed_sum
);
33 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
34 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
39 /* Taken and modified from tcpdump, Copyright belongs to them! */
47 do { if ((x) > 65535) \
54 sum = l_util.s[0] + l_util.s[1]; \
58 static inline u16
__in_cksum(const struct cksum_vec
*vec
, int veclen
)
61 int sum
= 0, mlen
= 0;
72 for (; veclen
!= 0; vec
++, veclen
--) {
76 w
= (const u16
*) (void *) vec
->ptr
;
79 s_util
.c
[1] = *(const u8
*) w
;
81 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
86 if ((1 & (unsigned long) w
) && (mlen
> 0)) {
89 s_util
.c
[0] = *(const u8
*) w
;
90 w
= (const u16
*) (void *) ((const u8
*) w
+ 1);
95 while ((mlen
-= 32) >= 0) {
96 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
97 sum
+= w
[4]; sum
+= w
[5]; sum
+= w
[6]; sum
+= w
[7];
98 sum
+= w
[8]; sum
+= w
[9]; sum
+= w
[10]; sum
+= w
[11];
99 sum
+= w
[12]; sum
+= w
[13]; sum
+= w
[14]; sum
+= w
[15];
105 while ((mlen
-= 8) >= 0) {
106 sum
+= w
[0]; sum
+= w
[1]; sum
+= w
[2]; sum
+= w
[3];
112 if (mlen
== 0 && byte_swapped
== 0)
117 while ((mlen
-= 2) >= 0) {
127 s_util
.c
[1] = *(const u8
*) w
;
132 } else if (mlen
== -1)
133 s_util
.c
[0] = *(const u8
*) w
;
143 return (~sum
& 0xffff);
146 static inline u16
p4_csum(const struct ip
*ip
, const u8
*data
, u16 len
,
149 struct cksum_vec vec
[2];
158 memset(&ph
, 0, sizeof(ph
));
161 ph
.proto
= next_proto
;
162 ph
.src
= ip
->ip_src
.s_addr
;
163 ph
.dst
= ip
->ip_dst
.s_addr
;
165 vec
[0].ptr
= (const u8
*) (void *) &ph
;
166 vec
[0].len
= sizeof(ph
);
171 return __in_cksum(vec
, 2);