]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/checksum.c
2 * BIRD Library -- IP One-Complement Checksum
4 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
10 * DOC: Miscellaneous functions.
15 #include "nest/bird.h"
22 // return z + (z < sum);
31 ipsum_calc_block(u32
*buf
, unsigned len
, u16 isum
)
34 * A few simple facts about the IP checksum (see RFC 1071 for detailed
37 * o It's associative and commutative.
38 * o It's byte order independent.
39 * o It's word size independent.
41 * This gives us a neat 32-bits-at-a-time algorithm which respects
42 * usual alignment requirements and is reasonably fast.
49 u32
*end
= buf
+ (len
>> 2);
52 sum
= add32(sum
, *buf
++);
54 sum
= (sum
>> 16) + (sum
& 0xffff); /* add high-16 to low-16 */
55 sum
+= (sum
>> 16); /* add carry */
60 ipsum_calc(void *frag
, unsigned len
, va_list args
)
66 sum
= ipsum_calc_block(frag
, len
, sum
);
67 frag
= va_arg(args
, void *);
70 len
= va_arg(args
, unsigned);
76 * ipsum_verify - verify an IP checksum
77 * @frag: first packet fragment
78 * @len: length in bytes
80 * This function verifies whether a given fragmented packet
81 * has correct one's complement checksum as used by the IP
84 * It uses all the clever tricks described in RFC 1071 to speed
85 * up checksum calculation as much as possible.
87 * Result: 1 if the checksum is correct, 0 else.
90 ipsum_verify(void *frag
, unsigned len
, ...)
96 sum
= ipsum_calc(frag
, len
, args
);
102 * ipsum_calculate - compute an IP checksum
103 * @frag: first packet fragment
104 * @len: length in bytes
106 * This function calculates a one's complement checksum of a given fragmented
109 * It uses all the clever tricks described in RFC 1071 to speed
110 * up checksum calculation as much as possible.
113 ipsum_calculate(void *frag
, unsigned len
, ...)
119 sum
= ipsum_calc(frag
, len
, args
);