]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
4528f34f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "csum.h" | |
80642190 | 19 | #include "unaligned.h" |
064af421 | 20 | |
6506f45c BP |
21 | #ifndef __CHECKER__ |
22 | ||
21effc03 BP |
23 | /* Returns the IP checksum of the 'n' bytes in 'data'. |
24 | * | |
25 | * The return value has the same endianness as the data. That is, if 'data' | |
26 | * consists of a packet in network byte order, then the return value is a value | |
27 | * in network byte order, and if 'data' consists of a data structure in host | |
28 | * byte order, then the return value is in host byte order. */ | |
6879df6b | 29 | ovs_be16 |
064af421 BP |
30 | csum(const void *data, size_t n) |
31 | { | |
32 | return csum_finish(csum_continue(0, data, n)); | |
33 | } | |
34 | ||
35 | /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns | |
36 | * the updated checksum. (To start a new checksum, pass 0 for 'partial'. To | |
37 | * obtain the finished checksum, pass the return value to csum_finish().) */ | |
38 | uint32_t | |
6879df6b | 39 | csum_add16(uint32_t partial, ovs_be16 new) |
064af421 BP |
40 | { |
41 | return partial + new; | |
42 | } | |
43 | ||
44 | /* Adds the 32 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 | |
6879df6b | 48 | csum_add32(uint32_t partial, ovs_be32 new) |
064af421 BP |
49 | { |
50 | return partial + (new >> 16) + (new & 0xffff); | |
51 | } | |
52 | ||
53 | ||
54 | /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and | |
55 | * returns the updated checksum. (To start a new checksum, pass 0 for | |
56 | * 'partial'. To obtain the finished checksum, pass the return value to | |
57 | * csum_finish().) */ | |
58 | uint32_t | |
59 | csum_continue(uint32_t partial, const void *data_, size_t n) | |
60 | { | |
6879df6b | 61 | const ovs_be16 *data = data_; |
064af421 | 62 | |
80642190 | 63 | for (; n > 1; n -= 2, data++) { |
6879df6b | 64 | partial = csum_add16(partial, get_unaligned_be16(data)); |
064af421 BP |
65 | } |
66 | if (n) { | |
67 | partial += *(uint8_t *) data; | |
68 | } | |
69 | return partial; | |
70 | } | |
71 | ||
72 | /* Returns the IP checksum corresponding to 'partial', which is a value updated | |
21effc03 BP |
73 | * by some combination of csum_add16(), csum_add32(), and csum_continue(). |
74 | * | |
75 | * The return value has the same endianness as the checksummed data. That is, | |
76 | * if the data consist of a packet in network byte order, then the return value | |
77 | * is a value in network byte order, and if the data are a data structure in | |
78 | * host byte order, then the return value is in host byte order. */ | |
6879df6b | 79 | ovs_be16 |
064af421 BP |
80 | csum_finish(uint32_t partial) |
81 | { | |
4787d6d7 BP |
82 | while (partial >> 16) { |
83 | partial = (partial & 0xffff) + (partial >> 16); | |
84 | } | |
85 | return ~partial; | |
064af421 BP |
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'. */ | |
6879df6b BP |
91 | ovs_be16 |
92 | recalc_csum16(ovs_be16 old_csum, ovs_be16 old_u16, ovs_be16 new_u16) | |
064af421 BP |
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; | |
4787d6d7 | 102 | return csum_finish(sum); |
064af421 BP |
103 | } |
104 | ||
105 | /* Returns the new checksum for a packet in which the checksum field previously | |
106 | * contained 'old_csum' and in which a field that contained 'old_u32' was | |
107 | * changed to contain 'new_u32'. */ | |
6879df6b BP |
108 | ovs_be16 |
109 | recalc_csum32(ovs_be16 old_csum, ovs_be32 old_u32, ovs_be32 new_u32) | |
064af421 BP |
110 | { |
111 | return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32), | |
112 | old_u32 >> 16, new_u32 >> 16); | |
113 | } | |
6506f45c | 114 | |
e60e935b SRCSA |
115 | /* Returns the new checksum for a packet in which the checksum field previously |
116 | * contained 'old_csum' and in which a field that contained the 6 bytes at | |
117 | * 'old_bytes' was changed to contain the 6 bytes at 'new_bytes'. */ | |
118 | ovs_be16 | |
119 | recalc_csum48(ovs_be16 old_csum, const void *old_bytes, | |
120 | const void *new_bytes) | |
121 | { | |
122 | ovs_be16 new_csum = old_csum; | |
123 | const uint16_t *p16_old = old_bytes, | |
124 | *p16_new = new_bytes; | |
125 | int i; | |
126 | ||
127 | for (i = 0; i < 3; ++i) { | |
128 | new_csum = recalc_csum16(new_csum, get_unaligned_be16(&p16_old[i]), | |
129 | get_unaligned_be16(&p16_new[i])); | |
130 | } | |
131 | ||
132 | return new_csum; | |
133 | } | |
134 | ||
bc7a5acd AA |
135 | /* Returns the new checksum for a packet in which the checksum field previously |
136 | * contained 'old_csum' and in which a field that contained 'old_u32[4]' was | |
137 | * changed to contain 'new_u32[4]'. */ | |
138 | ovs_be16 | |
4528f34f | 139 | recalc_csum128(ovs_be16 old_csum, ovs_16aligned_be32 old_u32[4], |
bc7a5acd AA |
140 | const ovs_be32 new_u32[4]) |
141 | { | |
142 | ovs_be16 new_csum = old_csum; | |
143 | int i; | |
144 | ||
145 | for (i = 0; i < 4; ++i) { | |
4528f34f BP |
146 | new_csum = recalc_csum32(new_csum, |
147 | get_16aligned_be32(&old_u32[i]), new_u32[i]); | |
bc7a5acd AA |
148 | } |
149 | return new_csum; | |
150 | } | |
6506f45c BP |
151 | #else /* __CHECKER__ */ |
152 | /* Making sparse happy with these functions also makes them unreadable, so | |
153 | * don't bother to show it their implementations. */ | |
154 | #endif |