2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Returns the IP checksum of the 'n' bytes in 'data'.
22 * The return value has the same endianness as the data. That is, if 'data'
23 * consists of a packet in network byte order, then the return value is a value
24 * in network byte order, and if 'data' consists of a data structure in host
25 * byte order, then the return value is in host byte order. */
27 csum(const void *data, size_t n)
29 return csum_finish(csum_continue(0, data, n));
32 /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns
33 * the updated checksum. (To start a new checksum, pass 0 for 'partial'. To
34 * obtain the finished checksum, pass the return value to csum_finish().) */
36 csum_add16(uint32_t partial, uint16_t new)
41 /* Adds the 32 bits in 'new' to the partial IP checksum 'partial' and returns
42 * the updated checksum. (To start a new checksum, pass 0 for 'partial'. To
43 * obtain the finished checksum, pass the return value to csum_finish().) */
45 csum_add32(uint32_t partial, uint32_t new)
47 return partial + (new >> 16) + (new & 0xffff);
51 /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and
52 * returns the updated checksum. (To start a new checksum, pass 0 for
53 * 'partial'. To obtain the finished checksum, pass the return value to
56 csum_continue(uint32_t partial, const void *data_, size_t n)
58 const uint16_t *data = data_;
60 for (; n > 1; n -= 2) {
61 partial = csum_add16(partial, *data++);
64 partial += *(uint8_t *) data;
69 /* Returns the IP checksum corresponding to 'partial', which is a value updated
70 * by some combination of csum_add16(), csum_add32(), and csum_continue().
72 * The return value has the same endianness as the checksummed data. That is,
73 * if the data consist of a packet in network byte order, then the return value
74 * is a value in network byte order, and if the data are a data structure in
75 * host byte order, then the return value is in host byte order. */
77 csum_finish(uint32_t partial)
79 return ~((partial & 0xffff) + (partial >> 16));
82 /* Returns the new checksum for a packet in which the checksum field previously
83 * contained 'old_csum' and in which a field that contained 'old_u16' was
84 * changed to contain 'new_u16'. */
86 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
88 /* Ones-complement arithmetic is endian-independent, so this code does not
89 * use htons() or ntohs().
91 * See RFC 1624 for formula and explanation. */
92 uint16_t hc_complement = ~old_csum;
93 uint16_t m_complement = ~old_u16;
94 uint16_t m_prime = new_u16;
95 uint32_t sum = hc_complement + m_complement + m_prime;
96 uint16_t hc_prime_complement = sum + (sum >> 16);
97 return ~hc_prime_complement;
100 /* Returns the new checksum for a packet in which the checksum field previously
101 * contained 'old_csum' and in which a field that contained 'old_u32' was
102 * changed to contain 'new_u32'. */
104 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
106 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
107 old_u32 >> 16, new_u32 >> 16);