net: improve UDP/TCP checksum computation.
[qemu.git] / net / checksum.c
1 /*
2 * IP checksumming functions.
3 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; under version 2 or later of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "qemu/osdep.h"
19 #include "qemu-common.h"
20 #include "net/checksum.h"
21 #include "net/eth.h"
22
23 uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
24 {
25 uint32_t sum = 0;
26 int i;
27
28 for (i = seq; i < seq + len; i++) {
29 if (i & 1) {
30 sum += (uint32_t)buf[i - seq];
31 } else {
32 sum += (uint32_t)buf[i - seq] << 8;
33 }
34 }
35 return sum;
36 }
37
38 uint16_t net_checksum_finish(uint32_t sum)
39 {
40 while (sum>>16)
41 sum = (sum & 0xFFFF)+(sum >> 16);
42 return ~sum;
43 }
44
45 uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
46 uint8_t *addrs, uint8_t *buf)
47 {
48 uint32_t sum = 0;
49
50 sum += net_checksum_add(length, buf); // payload
51 sum += net_checksum_add(8, addrs); // src + dst address
52 sum += proto + length; // protocol & length
53 return net_checksum_finish(sum);
54 }
55
56 void net_checksum_calculate(uint8_t *data, int length)
57 {
58 int ip_len;
59 struct ip_header *ip;
60
61 /*
62 * Note: We cannot assume "data" is aligned, so the all code uses
63 * some macros that take care of possible unaligned access for
64 * struct members (just in case).
65 */
66
67 /* Ensure data has complete L2 & L3 headers. */
68 if (length < (sizeof(struct eth_header) + sizeof(struct ip_header))) {
69 return;
70 }
71
72 ip = (struct ip_header *)(data + sizeof(struct eth_header));
73
74 if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
75 return; /* not IPv4 */
76 }
77
78 ip_len = lduw_be_p(&ip->ip_len);
79
80 /* Last, check that we have enough data for the all IP frame */
81 if (length < ip_len) {
82 return;
83 }
84
85 ip_len -= IP_HDR_GET_LEN(ip);
86
87 switch (ip->ip_p) {
88 case IP_PROTO_TCP:
89 {
90 uint16_t csum;
91 tcp_header *tcp = (tcp_header *)(ip + 1);
92
93 if (ip_len < sizeof(tcp_header)) {
94 return;
95 }
96
97 /* Set csum to 0 */
98 stw_he_p(&tcp->th_sum, 0);
99
100 csum = net_checksum_tcpudp(ip_len, ip->ip_p,
101 (uint8_t *)&ip->ip_src,
102 (uint8_t *)tcp);
103
104 /* Store computed csum */
105 stw_be_p(&tcp->th_sum, csum);
106
107 break;
108 }
109 case IP_PROTO_UDP:
110 {
111 uint16_t csum;
112 udp_header *udp = (udp_header *)(ip + 1);
113
114 if (ip_len < sizeof(udp_header)) {
115 return;
116 }
117
118 /* Set csum to 0 */
119 stw_he_p(&udp->uh_sum, 0);
120
121 csum = net_checksum_tcpudp(ip_len, ip->ip_p,
122 (uint8_t *)&ip->ip_src,
123 (uint8_t *)udp);
124
125 /* Store computed csum */
126 stw_be_p(&udp->uh_sum, csum);
127
128 break;
129 }
130 default:
131 /* Can't handle any other protocol */
132 break;
133 }
134 }
135
136 uint32_t
137 net_checksum_add_iov(const struct iovec *iov, const unsigned int iov_cnt,
138 uint32_t iov_off, uint32_t size, uint32_t csum_offset)
139 {
140 size_t iovec_off, buf_off;
141 unsigned int i;
142 uint32_t res = 0;
143
144 iovec_off = 0;
145 buf_off = 0;
146 for (i = 0; i < iov_cnt && size; i++) {
147 if (iov_off < (iovec_off + iov[i].iov_len)) {
148 size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
149 void *chunk_buf = iov[i].iov_base + (iov_off - iovec_off);
150
151 res += net_checksum_add_cont(len, chunk_buf, csum_offset);
152 csum_offset += len;
153
154 buf_off += len;
155 iov_off += len;
156 size -= len;
157 }
158 iovec_off += iov[i].iov_len;
159 }
160 return res;
161 }