For SNAT, don't store the pre-fragment L2 header before actions are applied.
[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 <config.h>
35 #include "csum.h"
36
37 /* Returns the IP checksum of the 'n' bytes in 'data'. */
38 uint16_t
39 csum(const void *data, size_t n)
40 {
41     return csum_finish(csum_continue(0, data, n));
42 }
43
44 /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns
45  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
46  * obtain the finished checksum, pass the return value to csum_finish().) */
47 uint32_t
48 csum_add16(uint32_t partial, uint16_t new)
49 {
50     return partial + new;
51 }
52
53 /* Adds the 32 bits in 'new' to the partial IP checksum 'partial' and returns
54  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
55  * obtain the finished checksum, pass the return value to csum_finish().) */
56 uint32_t
57 csum_add32(uint32_t partial, uint32_t new)
58 {
59     return partial + (new >> 16) + (new & 0xffff);
60 }
61
62
63 /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and
64  * returns the updated checksum.  (To start a new checksum, pass 0 for
65  * 'partial'.  To obtain the finished checksum, pass the return value to
66  * csum_finish().) */
67 uint32_t
68 csum_continue(uint32_t partial, const void *data_, size_t n)
69 {
70     const uint16_t *data = data_;
71
72     for (; n > 1; n -= 2) {
73         partial = csum_add16(partial, *data++);
74     }
75     if (n) {
76         partial += *(uint8_t *) data;
77     }
78     return partial;
79 }
80
81 /* Returns the IP checksum corresponding to 'partial', which is a value updated
82  * by some combination of csum_add16(), csum_add32(), and csum_continue(). */
83 uint16_t
84 csum_finish(uint32_t partial)
85 {
86     return ~((partial & 0xffff) + (partial >> 16));
87 }
88
89 /* Returns the new checksum for a packet in which the checksum field previously
90  * contained 'old_csum' and in which a field that contained 'old_u16' was
91  * changed to contain 'new_u16'. */
92 uint16_t
93 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
94 {
95     /* Ones-complement arithmetic is endian-independent, so this code does not
96      * use htons() or ntohs().
97      *
98      * See RFC 1624 for formula and explanation. */
99     uint16_t hc_complement = ~old_csum;
100     uint16_t m_complement = ~old_u16;
101     uint16_t m_prime = new_u16;
102     uint32_t sum = hc_complement + m_complement + m_prime;
103     uint16_t hc_prime_complement = sum + (sum >> 16);
104     return ~hc_prime_complement;
105 }
106
107 /* Returns the new checksum for a packet in which the checksum field previously
108  * contained 'old_csum' and in which a field that contained 'old_u32' was
109  * changed to contain 'new_u32'. */
110 uint16_t
111 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
112 {
113     return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
114                          old_u32 >> 16, new_u32 >> 16);
115 }