]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
d6c1bc0d | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2015 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" |
932c96b7 | 20 | #include <netinet/in.h> |
064af421 | 21 | |
6506f45c | 22 | #ifndef __CHECKER__ |
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 | ||
064af421 BP |
35 | /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and |
36 | * returns the updated checksum. (To start a new checksum, pass 0 for | |
37 | * 'partial'. To obtain the finished checksum, pass the return value to | |
38 | * csum_finish().) */ | |
39 | uint32_t | |
40 | csum_continue(uint32_t partial, const void *data_, size_t n) | |
41 | { | |
6879df6b | 42 | const ovs_be16 *data = data_; |
064af421 | 43 | |
80642190 | 44 | for (; n > 1; n -= 2, data++) { |
6879df6b | 45 | partial = csum_add16(partial, get_unaligned_be16(data)); |
064af421 BP |
46 | } |
47 | if (n) { | |
23b2a1ba DDP |
48 | #ifdef WORDS_BIGENDIAN |
49 | partial += (*(uint8_t *) data) << 8; | |
50 | #else | |
064af421 | 51 | partial += *(uint8_t *) data; |
23b2a1ba | 52 | #endif |
064af421 BP |
53 | } |
54 | return partial; | |
55 | } | |
56 | ||
57 | /* Returns the IP checksum corresponding to 'partial', which is a value updated | |
21effc03 BP |
58 | * by some combination of csum_add16(), csum_add32(), and csum_continue(). |
59 | * | |
60 | * The return value has the same endianness as the checksummed data. That is, | |
61 | * if the data consist of a packet in network byte order, then the return value | |
62 | * is a value in network byte order, and if the data are a data structure in | |
63 | * host byte order, then the return value is in host byte order. */ | |
6879df6b | 64 | ovs_be16 |
064af421 BP |
65 | csum_finish(uint32_t partial) |
66 | { | |
4787d6d7 BP |
67 | while (partial >> 16) { |
68 | partial = (partial & 0xffff) + (partial >> 16); | |
69 | } | |
70 | return ~partial; | |
064af421 BP |
71 | } |
72 | ||
73 | /* Returns the new checksum for a packet in which the checksum field previously | |
74 | * contained 'old_csum' and in which a field that contained 'old_u16' was | |
75 | * changed to contain 'new_u16'. */ | |
6879df6b BP |
76 | ovs_be16 |
77 | recalc_csum16(ovs_be16 old_csum, ovs_be16 old_u16, ovs_be16 new_u16) | |
064af421 BP |
78 | { |
79 | /* Ones-complement arithmetic is endian-independent, so this code does not | |
80 | * use htons() or ntohs(). | |
81 | * | |
82 | * See RFC 1624 for formula and explanation. */ | |
83 | uint16_t hc_complement = ~old_csum; | |
84 | uint16_t m_complement = ~old_u16; | |
85 | uint16_t m_prime = new_u16; | |
86 | uint32_t sum = hc_complement + m_complement + m_prime; | |
4787d6d7 | 87 | return csum_finish(sum); |
064af421 BP |
88 | } |
89 | ||
90 | /* Returns the new checksum for a packet in which the checksum field previously | |
91 | * contained 'old_csum' and in which a field that contained 'old_u32' was | |
92 | * changed to contain 'new_u32'. */ | |
6879df6b BP |
93 | ovs_be16 |
94 | recalc_csum32(ovs_be16 old_csum, ovs_be32 old_u32, ovs_be32 new_u32) | |
064af421 BP |
95 | { |
96 | return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32), | |
97 | old_u32 >> 16, new_u32 >> 16); | |
98 | } | |
6506f45c | 99 | |
e60e935b SRCSA |
100 | /* Returns the new checksum for a packet in which the checksum field previously |
101 | * contained 'old_csum' and in which a field that contained the 6 bytes at | |
74ff3298 | 102 | * 'old_mac' was changed to contain the 6 bytes at 'new_mac'. */ |
e60e935b | 103 | ovs_be16 |
74ff3298 JR |
104 | recalc_csum48(ovs_be16 old_csum, const struct eth_addr old_mac, |
105 | const struct eth_addr new_mac) | |
e60e935b SRCSA |
106 | { |
107 | ovs_be16 new_csum = old_csum; | |
e60e935b | 108 | |
74ff3298 JR |
109 | for (int i = 0; i < 3; ++i) { |
110 | new_csum = recalc_csum16(new_csum, old_mac.be16[i], new_mac.be16[i]); | |
e60e935b SRCSA |
111 | } |
112 | ||
113 | return new_csum; | |
114 | } | |
115 | ||
bc7a5acd AA |
116 | /* Returns the new checksum for a packet in which the checksum field previously |
117 | * contained 'old_csum' and in which a field that contained 'old_u32[4]' was | |
118 | * changed to contain 'new_u32[4]'. */ | |
119 | ovs_be16 | |
4528f34f | 120 | recalc_csum128(ovs_be16 old_csum, ovs_16aligned_be32 old_u32[4], |
932c96b7 | 121 | const struct in6_addr *new_in6) |
bc7a5acd AA |
122 | { |
123 | ovs_be16 new_csum = old_csum; | |
932c96b7 JR |
124 | #ifndef s6_addr32 |
125 | ovs_be32 new_u32[4]; | |
126 | memcpy(new_u32, new_in6, sizeof new_u32); | |
127 | #else | |
128 | const ovs_be32 *new_u32 = new_in6->s6_addr32; | |
129 | #endif | |
bc7a5acd AA |
130 | int i; |
131 | ||
132 | for (i = 0; i < 4; ++i) { | |
4528f34f BP |
133 | new_csum = recalc_csum32(new_csum, |
134 | get_16aligned_be32(&old_u32[i]), new_u32[i]); | |
bc7a5acd AA |
135 | } |
136 | return new_csum; | |
137 | } | |
6506f45c BP |
138 | #else /* __CHECKER__ */ |
139 | /* Making sparse happy with these functions also makes them unreadable, so | |
140 | * don't bother to show it their implementations. */ | |
141 | #endif |