2 * Copyright (c) 2008, 2009, 2010 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.
19 #include "unaligned.h"
21 /* Returns the IP checksum of the 'n' bytes in 'data'.
23 * The return value has the same endianness as the data. That is, if 'data'
24 * consists of a packet in network byte order, then the return value is a value
25 * in network byte order, and if 'data' consists of a data structure in host
26 * byte order, then the return value is in host byte order. */
28 csum(const void *data, size_t n)
30 return csum_finish(csum_continue(0, data, n));
33 /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns
34 * the updated checksum. (To start a new checksum, pass 0 for 'partial'. To
35 * obtain the finished checksum, pass the return value to csum_finish().) */
37 csum_add16(uint32_t partial, uint16_t new)
42 /* Adds the 32 bits in 'new' to the partial IP checksum 'partial' and returns
43 * the updated checksum. (To start a new checksum, pass 0 for 'partial'. To
44 * obtain the finished checksum, pass the return value to csum_finish().) */
46 csum_add32(uint32_t partial, uint32_t new)
48 return partial + (new >> 16) + (new & 0xffff);
52 /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and
53 * returns the updated checksum. (To start a new checksum, pass 0 for
54 * 'partial'. To obtain the finished checksum, pass the return value to
57 csum_continue(uint32_t partial, const void *data_, size_t n)
59 const uint16_t *data = data_;
61 for (; n > 1; n -= 2, data++) {
62 partial = csum_add16(partial, get_unaligned_u16(data));
65 partial += *(uint8_t *) data;
70 /* Returns the IP checksum corresponding to 'partial', which is a value updated
71 * by some combination of csum_add16(), csum_add32(), and csum_continue().
73 * The return value has the same endianness as the checksummed data. That is,
74 * if the data consist of a packet in network byte order, then the return value
75 * is a value in network byte order, and if the data are a data structure in
76 * host byte order, then the return value is in host byte order. */
78 csum_finish(uint32_t partial)
80 while (partial >> 16) {
81 partial = (partial & 0xffff) + (partial >> 16);
86 /* Returns the new checksum for a packet in which the checksum field previously
87 * contained 'old_csum' and in which a field that contained 'old_u16' was
88 * changed to contain 'new_u16'. */
90 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
92 /* Ones-complement arithmetic is endian-independent, so this code does not
93 * use htons() or ntohs().
95 * See RFC 1624 for formula and explanation. */
96 uint16_t hc_complement = ~old_csum;
97 uint16_t m_complement = ~old_u16;
98 uint16_t m_prime = new_u16;
99 uint32_t sum = hc_complement + m_complement + m_prime;
100 return csum_finish(sum);
103 /* Returns the new checksum for a packet in which the checksum field previously
104 * contained 'old_csum' and in which a field that contained 'old_u32' was
105 * changed to contain 'new_u32'. */
107 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
109 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
110 old_u32 >> 16, new_u32 >> 16);