ARM: ACPI: Add power button device in ACPI DSDT table
[qemu.git] / net / checksum.c
blob14c08550e0c88535a944615e2068bb890e3ea78e
1 /*
2 * IP checksumming functions.
3 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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/>.
18 #include "qemu-common.h"
19 #include "net/checksum.h"
21 #define PROTO_TCP 6
22 #define PROTO_UDP 17
24 uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
26 uint32_t sum = 0;
27 int i;
29 for (i = seq; i < seq + len; i++) {
30 if (i & 1) {
31 sum += (uint32_t)buf[i - seq];
32 } else {
33 sum += (uint32_t)buf[i - seq] << 8;
36 return sum;
39 uint16_t net_checksum_finish(uint32_t sum)
41 while (sum>>16)
42 sum = (sum & 0xFFFF)+(sum >> 16);
43 return ~sum;
46 uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
47 uint8_t *addrs, uint8_t *buf)
49 uint32_t sum = 0;
51 sum += net_checksum_add(length, buf); // payload
52 sum += net_checksum_add(8, addrs); // src + dst address
53 sum += proto + length; // protocol & length
54 return net_checksum_finish(sum);
57 void net_checksum_calculate(uint8_t *data, int length)
59 int hlen, plen, proto, csum_offset;
60 uint16_t csum;
62 if ((data[14] & 0xf0) != 0x40)
63 return; /* not IPv4 */
64 hlen = (data[14] & 0x0f) * 4;
65 plen = (data[16] << 8 | data[17]) - hlen;
66 proto = data[23];
68 switch (proto) {
69 case PROTO_TCP:
70 csum_offset = 16;
71 break;
72 case PROTO_UDP:
73 csum_offset = 6;
74 break;
75 default:
76 return;
79 if (plen < csum_offset+2)
80 return;
82 data[14+hlen+csum_offset] = 0;
83 data[14+hlen+csum_offset+1] = 0;
84 csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
85 data[14+hlen+csum_offset] = csum >> 8;
86 data[14+hlen+csum_offset+1] = csum & 0xff;
89 uint32_t
90 net_checksum_add_iov(const struct iovec *iov, const unsigned int iov_cnt,
91 uint32_t iov_off, uint32_t size)
93 size_t iovec_off, buf_off;
94 unsigned int i;
95 uint32_t res = 0;
96 uint32_t seq = 0;
98 iovec_off = 0;
99 buf_off = 0;
100 for (i = 0; i < iov_cnt && size; i++) {
101 if (iov_off < (iovec_off + iov[i].iov_len)) {
102 size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
103 void *chunk_buf = iov[i].iov_base + (iov_off - iovec_off);
105 res += net_checksum_add_cont(len, chunk_buf, seq);
106 seq += len;
108 buf_off += len;
109 iov_off += len;
110 size -= len;
112 iovec_off += iov[i].iov_len;
114 return res;