migration: delete QEMUFile stdio implementation
[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
22 #define PROTO_TCP 6
23 #define PROTO_UDP 17
24
25 uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
26 {
27 uint32_t sum = 0;
28 int i;
29
30 for (i = seq; i < seq + len; i++) {
31 if (i & 1) {
32 sum += (uint32_t)buf[i - seq];
33 } else {
34 sum += (uint32_t)buf[i - seq] << 8;
35 }
36 }
37 return sum;
38 }
39
40 uint16_t net_checksum_finish(uint32_t sum)
41 {
42 while (sum>>16)
43 sum = (sum & 0xFFFF)+(sum >> 16);
44 return ~sum;
45 }
46
47 uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
48 uint8_t *addrs, uint8_t *buf)
49 {
50 uint32_t sum = 0;
51
52 sum += net_checksum_add(length, buf); // payload
53 sum += net_checksum_add(8, addrs); // src + dst address
54 sum += proto + length; // protocol & length
55 return net_checksum_finish(sum);
56 }
57
58 void net_checksum_calculate(uint8_t *data, int length)
59 {
60 int hlen, plen, proto, csum_offset;
61 uint16_t csum;
62
63 /* Ensure data has complete L2 & L3 headers. */
64 if (length < 14 + 20) {
65 return;
66 }
67
68 if ((data[14] & 0xf0) != 0x40)
69 return; /* not IPv4 */
70 hlen = (data[14] & 0x0f) * 4;
71 plen = (data[16] << 8 | data[17]) - hlen;
72 proto = data[23];
73
74 switch (proto) {
75 case PROTO_TCP:
76 csum_offset = 16;
77 break;
78 case PROTO_UDP:
79 csum_offset = 6;
80 break;
81 default:
82 return;
83 }
84
85 if (plen < csum_offset + 2 || 14 + hlen + plen > length) {
86 return;
87 }
88
89 data[14+hlen+csum_offset] = 0;
90 data[14+hlen+csum_offset+1] = 0;
91 csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
92 data[14+hlen+csum_offset] = csum >> 8;
93 data[14+hlen+csum_offset+1] = csum & 0xff;
94 }
95
96 uint32_t
97 net_checksum_add_iov(const struct iovec *iov, const unsigned int iov_cnt,
98 uint32_t iov_off, uint32_t size)
99 {
100 size_t iovec_off, buf_off;
101 unsigned int i;
102 uint32_t res = 0;
103 uint32_t seq = 0;
104
105 iovec_off = 0;
106 buf_off = 0;
107 for (i = 0; i < iov_cnt && size; i++) {
108 if (iov_off < (iovec_off + iov[i].iov_len)) {
109 size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
110 void *chunk_buf = iov[i].iov_base + (iov_off - iovec_off);
111
112 res += net_checksum_add_cont(len, chunk_buf, seq);
113 seq += len;
114
115 buf_off += len;
116 iov_off += len;
117 size -= len;
118 }
119 iovec_off += iov[i].iov_len;
120 }
121 return res;
122 }