]>
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"
18 static u16
/* One-complement addition */
33 ipsum_calc_block(u16
*x
, unsigned len
, u16 sum
)
39 * A few simple facts about the IP checksum (see RFC 1071 for detailed
42 * o It's associative and commutative.
43 * o It's byte order independent.
44 * o It's word size independent.
46 * This gives us a neat 32-bits-at-a-time algorithm which respects
47 * usual alignment requirements and is reasonably fast.
54 if ((unsigned long) x
& 2) /* Align to 32-bit boundary */
56 sum
= add16(sum
, *x
++);
65 tmp
= add32(tmp
, *xx
++);
68 sum
= add16(sum
, add16(tmp
& 0xffff, tmp
>> 16U));
70 sum
= add16(sum
, *(u16
*) xx
);
75 ipsum_calc(void *frag
, unsigned len
, va_list args
)
81 sum
= ipsum_calc_block(frag
, len
, sum
);
82 frag
= va_arg(args
, void *);
85 len
= va_arg(args
, unsigned);
91 * ipsum_verify - verify an IP checksum
92 * @frag: first packet fragment
93 * @len: length in bytes
95 * This function verifies whether a given fragmented packet
96 * has correct one's complement checksum as used by the IP
99 * It uses all the clever tricks described in RFC 1071 to speed
100 * up checksum calculation as much as possible.
102 * Result: 1 if the checksum is correct, 0 else.
105 ipsum_verify(void *frag
, unsigned len
, ...)
111 sum
= ipsum_calc(frag
, len
, args
);
113 return sum
== 0xffff;
117 * ipsum_calculate - compute an IP checksum
118 * @frag: first packet fragment
119 * @len: length in bytes
121 * This function calculates a one's complement checksum of a given fragmented
124 * It uses all the clever tricks described in RFC 1071 to speed
125 * up checksum calculation as much as possible.
128 ipsum_calculate(void *frag
, unsigned len
, ...)
134 sum
= ipsum_calc(frag
, len
, args
);