2
* Copyright (c) 2009 Lukas Mejdrech
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
* General CRC and checksum computation.
37
#ifndef __NET_CHECKSUM_H__
38
#define __NET_CHECKSUM_H__
40
#include <byteorder.h>
42
#include <sys/types.h>
44
/** IP checksum value for computed zero checksum.
45
* Zero is returned as 0xFFFF (not flipped)
47
#define IP_CHECKSUM_ZERO 0xFFFFu
49
/** Computes CRC32 value.
50
* @param[in] seed Initial value. Often used as 0 or ~0.
51
* @param[in] data Pointer to the beginning of data to process.
52
* @param[in] length Length of the data in bits.
53
* @returns The computed CRC32 of the length bits of the data.
55
#ifdef ARCH_IS_BIG_ENDIAN
56
#define compute_crc32( seed, data, length ) compute_crc32_be( seed, ( uint8_t * ) data, length )
58
#define compute_crc32( seed, data, length ) compute_crc32_le( seed, ( uint8_t * ) data, length )
61
/** Computes CRC32 value in the little-endian environment.
62
* @param[in] seed Initial value. Often used as 0 or ~0.
63
* @param[in] data Pointer to the beginning of data to process.
64
* @param[in] length Length of the data in bits.
65
* @returns The computed CRC32 of the length bits of the data.
67
uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length );
69
/** Computes CRC32 value in the big-endian environment.
70
* @param[in] seed Initial value. Often used as 0 or ~0.
71
* @param[in] data Pointer to the beginning of data to process.
72
* @param[in] length Length of the data in bits.
73
* @returns The computed CRC32 of the length bits of the data.
75
uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length );
77
/** Computes sum of the 2 byte fields.
78
* Padds one zero (0) byte if odd.
79
* @param[in] seed Initial value. Often used as 0 or ~0.
80
* @param[in] data Pointer to the beginning of data to process.
81
* @param[in] length Length of the data in bytes.
82
* @returns The computed checksum of the length bytes of the data.
84
uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length );
86
/** Compacts the computed checksum to the 16 bit number adding the carries.
87
* @param[in] sum Computed checksum.
88
* @returns Compacted computed checksum to the 16 bits.
90
uint16_t compact_checksum( uint32_t sum );
92
/** Returns or flips the checksum if zero.
93
* @param[in] checksum The computed checksum.
94
* @returns The internet protocol header checksum.
95
* @returns 0xFFFF if the computed checksum is zero.
97
uint16_t flip_checksum( uint16_t checksum );
99
/** Computes the ip header checksum.
100
* To compute the checksum of a new packet, the checksum header field must be zero.
101
* To check the checksum of a received packet, the checksum may be left set.
102
* The zero (0) value will be returned in this case if valid.
103
* @param[in] data The header data.
104
* @param[in] length The header length in bytes.
105
* @returns The internet protocol header checksum.
106
* @returns 0xFFFF if the computed checksum is zero.
108
uint16_t ip_checksum( uint8_t * data, size_t length );