2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 while (partial >> 16) {
80 partial = (partial & 0xffff) + (partial >> 16);
85 /* Returns the new checksum for a packet in which the checksum field previously
86 * contained 'old_csum' and in which a field that contained 'old_u16' was
87 * changed to contain 'new_u16'. */
89 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
91 /* Ones-complement arithmetic is endian-independent, so this code does not
92 * use htons() or ntohs().
94 * See RFC 1624 for formula and explanation. */
95 uint16_t hc_complement = ~old_csum;
96 uint16_t m_complement = ~old_u16;
97 uint16_t m_prime = new_u16;
98 uint32_t sum = hc_complement + m_complement + m_prime;
99 return csum_finish(sum);
102 /* Returns the new checksum for a packet in which the checksum field previously
103 * contained 'old_csum' and in which a field that contained 'old_u32' was
104 * changed to contain 'new_u32'. */
106 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
108 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
109 old_u32 >> 16, new_u32 >> 16);