]> git.proxmox.com Git - mirror_ovs.git/blob - lib/csum.c
csum: Avoid misaligned data access.
[mirror_ovs.git] / lib / csum.c
1 /*
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3 *
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:
7 *
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.
15 */
16
17 #include <config.h>
18 #include "csum.h"
19 #include "unaligned.h"
20
21 /* Returns the IP checksum of the 'n' bytes in 'data'.
22 *
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. */
27 uint16_t
28 csum(const void *data, size_t n)
29 {
30 return csum_finish(csum_continue(0, data, n));
31 }
32
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().) */
36 uint32_t
37 csum_add16(uint32_t partial, uint16_t new)
38 {
39 return partial + new;
40 }
41
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().) */
45 uint32_t
46 csum_add32(uint32_t partial, uint32_t new)
47 {
48 return partial + (new >> 16) + (new & 0xffff);
49 }
50
51
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
55 * csum_finish().) */
56 uint32_t
57 csum_continue(uint32_t partial, const void *data_, size_t n)
58 {
59 const uint16_t *data = data_;
60
61 for (; n > 1; n -= 2, data++) {
62 partial = csum_add16(partial, get_unaligned_u16(data));
63 }
64 if (n) {
65 partial += *(uint8_t *) data;
66 }
67 return partial;
68 }
69
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().
72 *
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. */
77 uint16_t
78 csum_finish(uint32_t partial)
79 {
80 while (partial >> 16) {
81 partial = (partial & 0xffff) + (partial >> 16);
82 }
83 return ~partial;
84 }
85
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'. */
89 uint16_t
90 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
91 {
92 /* Ones-complement arithmetic is endian-independent, so this code does not
93 * use htons() or ntohs().
94 *
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);
101 }
102
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'. */
106 uint16_t
107 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
108 {
109 return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
110 old_u32 >> 16, new_u32 >> 16);
111 }