Statistics
| Branch: | Revision:

root / net / checksum.c @ 067404be

History | View | Annotate | Download (3 kB)

1 7200ac3c Mark McLoughlin
/*
2 7200ac3c Mark McLoughlin
 *  IP checksumming functions.
3 7200ac3c Mark McLoughlin
 *  (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
4 7200ac3c Mark McLoughlin
 *
5 7200ac3c Mark McLoughlin
 *  This program is free software; you can redistribute it and/or modify
6 7200ac3c Mark McLoughlin
 *  it under the terms of the GNU General Public License as published by
7 4c32fe66 Stefan Weil
 *  the Free Software Foundation; under version 2 or later of the License.
8 7200ac3c Mark McLoughlin
 *
9 7200ac3c Mark McLoughlin
 *  This program is distributed in the hope that it will be useful,
10 7200ac3c Mark McLoughlin
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 7200ac3c Mark McLoughlin
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 7200ac3c Mark McLoughlin
 *  GNU General Public License for more details.
13 7200ac3c Mark McLoughlin
 *
14 7200ac3c Mark McLoughlin
 *  You should have received a copy of the GNU General Public License
15 7200ac3c Mark McLoughlin
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 7200ac3c Mark McLoughlin
 */
17 7200ac3c Mark McLoughlin
18 84026301 Dmitry Fleytman
#include "qemu-common.h"
19 7200ac3c Mark McLoughlin
#include "net/checksum.h"
20 7200ac3c Mark McLoughlin
21 7200ac3c Mark McLoughlin
#define PROTO_TCP  6
22 7200ac3c Mark McLoughlin
#define PROTO_UDP 17
23 7200ac3c Mark McLoughlin
24 5acf5ea4 Dmitry Fleytman
uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq)
25 7200ac3c Mark McLoughlin
{
26 7200ac3c Mark McLoughlin
    uint32_t sum = 0;
27 7200ac3c Mark McLoughlin
    int i;
28 7200ac3c Mark McLoughlin
29 5acf5ea4 Dmitry Fleytman
    for (i = seq; i < seq + len; i++) {
30 5acf5ea4 Dmitry Fleytman
        if (i & 1) {
31 5acf5ea4 Dmitry Fleytman
            sum += (uint32_t)buf[i - seq];
32 5acf5ea4 Dmitry Fleytman
        } else {
33 5acf5ea4 Dmitry Fleytman
            sum += (uint32_t)buf[i - seq] << 8;
34 5acf5ea4 Dmitry Fleytman
        }
35 7200ac3c Mark McLoughlin
    }
36 7200ac3c Mark McLoughlin
    return sum;
37 7200ac3c Mark McLoughlin
}
38 7200ac3c Mark McLoughlin
39 7200ac3c Mark McLoughlin
uint16_t net_checksum_finish(uint32_t sum)
40 7200ac3c Mark McLoughlin
{
41 7200ac3c Mark McLoughlin
    while (sum>>16)
42 7200ac3c Mark McLoughlin
        sum = (sum & 0xFFFF)+(sum >> 16);
43 7200ac3c Mark McLoughlin
    return ~sum;
44 7200ac3c Mark McLoughlin
}
45 7200ac3c Mark McLoughlin
46 7200ac3c Mark McLoughlin
uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
47 7200ac3c Mark McLoughlin
                             uint8_t *addrs, uint8_t *buf)
48 7200ac3c Mark McLoughlin
{
49 7200ac3c Mark McLoughlin
    uint32_t sum = 0;
50 7200ac3c Mark McLoughlin
51 7200ac3c Mark McLoughlin
    sum += net_checksum_add(length, buf);         // payload
52 7200ac3c Mark McLoughlin
    sum += net_checksum_add(8, addrs);            // src + dst address
53 7200ac3c Mark McLoughlin
    sum += proto + length;                        // protocol & length
54 7200ac3c Mark McLoughlin
    return net_checksum_finish(sum);
55 7200ac3c Mark McLoughlin
}
56 7200ac3c Mark McLoughlin
57 7200ac3c Mark McLoughlin
void net_checksum_calculate(uint8_t *data, int length)
58 7200ac3c Mark McLoughlin
{
59 7200ac3c Mark McLoughlin
    int hlen, plen, proto, csum_offset;
60 7200ac3c Mark McLoughlin
    uint16_t csum;
61 7200ac3c Mark McLoughlin
62 7200ac3c Mark McLoughlin
    if ((data[14] & 0xf0) != 0x40)
63 7200ac3c Mark McLoughlin
        return; /* not IPv4 */
64 7200ac3c Mark McLoughlin
    hlen  = (data[14] & 0x0f) * 4;
65 7200ac3c Mark McLoughlin
    plen  = (data[16] << 8 | data[17]) - hlen;
66 7200ac3c Mark McLoughlin
    proto = data[23];
67 7200ac3c Mark McLoughlin
68 7200ac3c Mark McLoughlin
    switch (proto) {
69 7200ac3c Mark McLoughlin
    case PROTO_TCP:
70 7200ac3c Mark McLoughlin
        csum_offset = 16;
71 7200ac3c Mark McLoughlin
        break;
72 7200ac3c Mark McLoughlin
    case PROTO_UDP:
73 7200ac3c Mark McLoughlin
        csum_offset = 6;
74 7200ac3c Mark McLoughlin
        break;
75 7200ac3c Mark McLoughlin
    default:
76 7200ac3c Mark McLoughlin
        return;
77 7200ac3c Mark McLoughlin
    }
78 7200ac3c Mark McLoughlin
79 7200ac3c Mark McLoughlin
    if (plen < csum_offset+2)
80 7200ac3c Mark McLoughlin
        return;
81 7200ac3c Mark McLoughlin
82 7200ac3c Mark McLoughlin
    data[14+hlen+csum_offset]   = 0;
83 7200ac3c Mark McLoughlin
    data[14+hlen+csum_offset+1] = 0;
84 7200ac3c Mark McLoughlin
    csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
85 7200ac3c Mark McLoughlin
    data[14+hlen+csum_offset]   = csum >> 8;
86 7200ac3c Mark McLoughlin
    data[14+hlen+csum_offset+1] = csum & 0xff;
87 7200ac3c Mark McLoughlin
}
88 84026301 Dmitry Fleytman
89 84026301 Dmitry Fleytman
uint32_t
90 84026301 Dmitry Fleytman
net_checksum_add_iov(const struct iovec *iov, const unsigned int iov_cnt,
91 84026301 Dmitry Fleytman
                     uint32_t iov_off, uint32_t size)
92 84026301 Dmitry Fleytman
{
93 84026301 Dmitry Fleytman
    size_t iovec_off, buf_off;
94 84026301 Dmitry Fleytman
    unsigned int i;
95 84026301 Dmitry Fleytman
    uint32_t res = 0;
96 84026301 Dmitry Fleytman
    uint32_t seq = 0;
97 84026301 Dmitry Fleytman
98 84026301 Dmitry Fleytman
    iovec_off = 0;
99 84026301 Dmitry Fleytman
    buf_off = 0;
100 84026301 Dmitry Fleytman
    for (i = 0; i < iov_cnt && size; i++) {
101 84026301 Dmitry Fleytman
        if (iov_off < (iovec_off + iov[i].iov_len)) {
102 84026301 Dmitry Fleytman
            size_t len = MIN((iovec_off + iov[i].iov_len) - iov_off , size);
103 84026301 Dmitry Fleytman
            void *chunk_buf = iov[i].iov_base + (iov_off - iovec_off);
104 84026301 Dmitry Fleytman
105 84026301 Dmitry Fleytman
            res += net_checksum_add_cont(len, chunk_buf, seq);
106 84026301 Dmitry Fleytman
            seq += len;
107 84026301 Dmitry Fleytman
108 84026301 Dmitry Fleytman
            buf_off += len;
109 84026301 Dmitry Fleytman
            iov_off += len;
110 84026301 Dmitry Fleytman
            size -= len;
111 84026301 Dmitry Fleytman
        }
112 84026301 Dmitry Fleytman
        iovec_off += iov[i].iov_len;
113 84026301 Dmitry Fleytman
    }
114 84026301 Dmitry Fleytman
    return res;
115 84026301 Dmitry Fleytman
}