]> git.proxmox.com Git - mirror_qemu.git/blame - net/checksum.c
iscsi: drop unused IscsiAIOCB.qiov field
[mirror_qemu.git] / net / checksum.c
CommitLineData
7200ac3c
MM
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
4c32fe66 7 * the Free Software Foundation; under version 2 or later of the License.
7200ac3c
MM
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
2744d920 18#include "qemu/osdep.h"
84026301 19#include "qemu-common.h"
7200ac3c 20#include "net/checksum.h"
50dbce65 21#include "net/eth.h"
7200ac3c 22
5acf5ea4 23uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
7200ac3c 24{
d5aa3e6e 25 uint32_t sum1 = 0, sum2 = 0;
7200ac3c
MM
26 int i;
27
d5aa3e6e
LP
28 for (i = 0; i < len - 1; i += 2) {
29 sum1 += (uint32_t)buf[i];
30 sum2 += (uint32_t)buf[i + 1];
31 }
32 if (i < len) {
33 sum1 += (uint32_t)buf[i];
34 }
35
36 if (seq & 1) {
37 return sum1 + (sum2 << 8);
38 } else {
39 return sum2 + (sum1 << 8);
7200ac3c 40 }
7200ac3c
MM
41}
42
43uint16_t net_checksum_finish(uint32_t sum)
44{
45 while (sum>>16)
46 sum = (sum & 0xFFFF)+(sum >> 16);
47 return ~sum;
48}
49
50uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
51 uint8_t *addrs, uint8_t *buf)
52{
53 uint32_t sum = 0;
54
55 sum += net_checksum_add(length, buf); // payload
56 sum += net_checksum_add(8, addrs); // src + dst address
57 sum += proto + length; // protocol & length
58 return net_checksum_finish(sum);
59}
60
61void net_checksum_calculate(uint8_t *data, int length)
62{
ade6bad1 63 int mac_hdr_len, ip_len;
50dbce65
JCD
64 struct ip_header *ip;
65
66 /*
67 * Note: We cannot assume "data" is aligned, so the all code uses
68 * some macros that take care of possible unaligned access for
69 * struct members (just in case).
70 */
7200ac3c 71
ade6bad1
JCD
72 /* Ensure we have at least an Eth header */
73 if (length < sizeof(struct eth_header)) {
362786f1
PP
74 return;
75 }
76
ade6bad1
JCD
77 /* Handle the optionnal VLAN headers */
78 switch (lduw_be_p(&PKT_GET_ETH_HDR(data)->h_proto)) {
79 case ETH_P_VLAN:
80 mac_hdr_len = sizeof(struct eth_header) +
81 sizeof(struct vlan_header);
82 break;
83 case ETH_P_DVLAN:
84 if (lduw_be_p(&PKT_GET_VLAN_HDR(data)->h_proto) == ETH_P_VLAN) {
85 mac_hdr_len = sizeof(struct eth_header) +
86 2 * sizeof(struct vlan_header);
87 } else {
88 mac_hdr_len = sizeof(struct eth_header) +
89 sizeof(struct vlan_header);
90 }
91 break;
92 default:
93 mac_hdr_len = sizeof(struct eth_header);
94 break;
95 }
96
97 length -= mac_hdr_len;
98
99 /* Now check we have an IP header (with an optionnal VLAN header) */
100 if (length < sizeof(struct ip_header)) {
101 return;
102 }
103
104 ip = (struct ip_header *)(data + mac_hdr_len);
50dbce65
JCD
105
106 if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
107 return; /* not IPv4 */
7200ac3c
MM
108 }
109
50dbce65
JCD
110 ip_len = lduw_be_p(&ip->ip_len);
111
112 /* Last, check that we have enough data for the all IP frame */
113 if (length < ip_len) {
362786f1
PP
114 return;
115 }
7200ac3c 116
50dbce65
JCD
117 ip_len -= IP_HDR_GET_LEN(ip);
118
119 switch (ip->ip_p) {
120 case IP_PROTO_TCP:
121 {
122 uint16_t csum;
123 tcp_header *tcp = (tcp_header *)(ip + 1);
124
125 if (ip_len < sizeof(tcp_header)) {
126 return;
127 }
128
129 /* Set csum to 0 */
130 stw_he_p(&tcp->th_sum, 0);
131
132 csum = net_checksum_tcpudp(ip_len, ip->ip_p,
133 (uint8_t *)&ip->ip_src,
134 (uint8_t *)tcp);
135
136 /* Store computed csum */
137 stw_be_p(&tcp->th_sum, csum);
138
139 break;
140 }
141 case IP_PROTO_UDP:
142 {
143 uint16_t csum;
144 udp_header *udp = (udp_header *)(ip + 1);
145
146 if (ip_len < sizeof(udp_header)) {
147 return;
148 }
149
150 /* Set csum to 0 */
151 stw_he_p(&udp->uh_sum, 0);
152
153 csum = net_checksum_tcpudp(ip_len, ip->ip_p,
154 (uint8_t *)&ip->ip_src,
155 (uint8_t *)udp);
156
157 /* Store computed csum */
158 stw_be_p(&udp->uh_sum, csum);
159
160 break;
161 }
162 default:
163 /* Can't handle any other protocol */
164 break;
165 }
7200ac3c 166}
84026301
DF
167
168uint32_t
169net_checksum_add_iov(const struct iovec *iov, const unsigned int iov_cnt,
eb700029 170 uint32_t iov_off, uint32_t size, uint32_t csum_offset)
84026301
DF
171{
172 size_t iovec_off, buf_off;
173 unsigned int i;
174 uint32_t res = 0;
84026301
DF
175
176 iovec_off = 0;
177 buf_off = 0;
178 for (i = 0; i < iov_cnt && size; i++) {
179 if (iov_off < (iovec_off + iov[i].iov_len)) {
180 size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
181 void *chunk_buf = iov[i].iov_base + (iov_off - iovec_off);
182
eb700029
DF
183 res += net_checksum_add_cont(len, chunk_buf, csum_offset);
184 csum_offset += len;
84026301
DF
185
186 buf_off += len;
187 iov_off += len;
188 size -= len;
189 }
190 iovec_off += iov[i].iov_len;
191 }
192 return res;
193}