6e9b06b028fec7a6839475a281c5444d9b4fdb98
[sliver-openvswitch.git] / lib / csum.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include "csum.h"
35
36 /* Returns the IP checksum of the 'n' bytes in 'data'. */
37 uint16_t
38 csum(const void *data, size_t n)
39 {
40     return csum_finish(csum_continue(0, data, n));
41 }
42
43 /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns
44  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
45  * obtain the finished checksum, pass the return value to csum_finish().) */
46 uint32_t
47 csum_add16(uint32_t partial, uint16_t new)
48 {
49     return partial + new;
50 }
51
52 /* Adds the 32 bits in 'new' to the partial IP checksum 'partial' and returns
53  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
54  * obtain the finished checksum, pass the return value to csum_finish().) */
55 uint32_t
56 csum_add32(uint32_t partial, uint32_t new)
57 {
58     return partial + (new >> 16) + (new & 0xffff);
59 }
60
61
62 /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and
63  * returns the updated checksum.  (To start a new checksum, pass 0 for
64  * 'partial'.  To obtain the finished checksum, pass the return value to
65  * csum_finish().) */
66 uint32_t
67 csum_continue(uint32_t partial, const void *data_, size_t n)
68 {
69     const uint16_t *data = data_;
70
71     for (; n > 1; n -= 2) {
72         partial = csum_add16(partial, *data++);
73     }
74     if (n) {
75         partial += *(uint8_t *) data;
76     }
77     return partial;
78 }
79
80 /* Returns the IP checksum corresponding to 'partial', which is a value updated
81  * by some combination of csum_add16(), csum_add32(), and csum_continue(). */
82 uint16_t
83 csum_finish(uint32_t partial)
84 {
85     return ~((partial & 0xffff) + (partial >> 16));
86 }
87
88 /* Returns the new checksum for a packet in which the checksum field previously
89  * contained 'old_csum' and in which a field that contained 'old_u16' was
90  * changed to contain 'new_u16'. */
91 uint16_t
92 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
93 {
94     /* Ones-complement arithmetic is endian-independent, so this code does not
95      * use htons() or ntohs().
96      *
97      * See RFC 1624 for formula and explanation. */
98     uint16_t hc_complement = ~old_csum;
99     uint16_t m_complement = ~old_u16;
100     uint16_t m_prime = new_u16;
101     uint32_t sum = hc_complement + m_complement + m_prime;
102     uint16_t hc_prime_complement = sum + (sum >> 16);
103     return ~hc_prime_complement;
104 }
105
106 /* Returns the new checksum for a packet in which the checksum field previously
107  * contained 'old_csum' and in which a field that contained 'old_u32' was
108  * changed to contain 'new_u32'. */
109 uint16_t
110 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
111 {
112     return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
113                          old_u32 >> 16, new_u32 >> 16);
114 }