2 * Copyright (c) 2008 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 csum(const void *data, size_t n)
24 return csum_finish(csum_continue(0, data, n));
27 /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns
28 * the updated checksum. (To start a new checksum, pass 0 for 'partial'. To
29 * obtain the finished checksum, pass the return value to csum_finish().) */
31 csum_add16(uint32_t partial, uint16_t new)
36 /* Adds the 32 bits in 'new' to the partial IP checksum 'partial' and returns
37 * the updated checksum. (To start a new checksum, pass 0 for 'partial'. To
38 * obtain the finished checksum, pass the return value to csum_finish().) */
40 csum_add32(uint32_t partial, uint32_t new)
42 return partial + (new >> 16) + (new & 0xffff);
46 /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and
47 * returns the updated checksum. (To start a new checksum, pass 0 for
48 * 'partial'. To obtain the finished checksum, pass the return value to
51 csum_continue(uint32_t partial, const void *data_, size_t n)
53 const uint16_t *data = data_;
55 for (; n > 1; n -= 2) {
56 partial = csum_add16(partial, *data++);
59 partial += *(uint8_t *) data;
64 /* Returns the IP checksum corresponding to 'partial', which is a value updated
65 * by some combination of csum_add16(), csum_add32(), and csum_continue(). */
67 csum_finish(uint32_t partial)
69 return ~((partial & 0xffff) + (partial >> 16));
72 /* Returns the new checksum for a packet in which the checksum field previously
73 * contained 'old_csum' and in which a field that contained 'old_u16' was
74 * changed to contain 'new_u16'. */
76 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
78 /* Ones-complement arithmetic is endian-independent, so this code does not
79 * use htons() or ntohs().
81 * See RFC 1624 for formula and explanation. */
82 uint16_t hc_complement = ~old_csum;
83 uint16_t m_complement = ~old_u16;
84 uint16_t m_prime = new_u16;
85 uint32_t sum = hc_complement + m_complement + m_prime;
86 uint16_t hc_prime_complement = sum + (sum >> 16);
87 return ~hc_prime_complement;
90 /* Returns the new checksum for a packet in which the checksum field previously
91 * contained 'old_csum' and in which a field that contained 'old_u32' was
92 * changed to contain 'new_u32'. */
94 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
96 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
97 old_u32 >> 16, new_u32 >> 16);