root / net-checksum.c @ 067d01de
History | View | Annotate | Download (2.2 kB)
1 | 48c64363 | aliguori | /*
|
---|---|---|---|
2 | 48c64363 | aliguori | * IP checksumming functions.
|
3 | 48c64363 | aliguori | * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
|
4 | 48c64363 | aliguori | *
|
5 | 48c64363 | aliguori | * This program is free software; you can redistribute it and/or modify
|
6 | 48c64363 | aliguori | * it under the terms of the GNU General Public License as published by
|
7 | 48c64363 | aliguori | * the Free Software Foundation; under version 2 of the License.
|
8 | 48c64363 | aliguori | *
|
9 | 48c64363 | aliguori | * This program is distributed in the hope that it will be useful,
|
10 | 48c64363 | aliguori | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 | 48c64363 | aliguori | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 | 48c64363 | aliguori | * GNU General Public License for more details.
|
13 | 48c64363 | aliguori | *
|
14 | 48c64363 | aliguori | * You should have received a copy of the GNU General Public License
|
15 | 8167ee88 | Blue Swirl | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
16 | 48c64363 | aliguori | */
|
17 | 48c64363 | aliguori | |
18 | 48c64363 | aliguori | #include "hw/hw.h" |
19 | 48c64363 | aliguori | #include "net.h" |
20 | 48c64363 | aliguori | |
21 | 48c64363 | aliguori | #define PROTO_TCP 6 |
22 | 48c64363 | aliguori | #define PROTO_UDP 17 |
23 | 48c64363 | aliguori | |
24 | 48c64363 | aliguori | uint32_t net_checksum_add(int len, uint8_t *buf)
|
25 | 48c64363 | aliguori | { |
26 | 48c64363 | aliguori | uint32_t sum = 0;
|
27 | 48c64363 | aliguori | int i;
|
28 | 48c64363 | aliguori | |
29 | 48c64363 | aliguori | for (i = 0; i < len; i++) { |
30 | 48c64363 | aliguori | if (i & 1) |
31 | 48c64363 | aliguori | sum += (uint32_t)buf[i]; |
32 | 48c64363 | aliguori | else
|
33 | 48c64363 | aliguori | sum += (uint32_t)buf[i] << 8;
|
34 | 48c64363 | aliguori | } |
35 | 48c64363 | aliguori | return sum;
|
36 | 48c64363 | aliguori | } |
37 | 48c64363 | aliguori | |
38 | 48c64363 | aliguori | uint16_t net_checksum_finish(uint32_t sum) |
39 | 48c64363 | aliguori | { |
40 | 48c64363 | aliguori | while (sum>>16) |
41 | 48c64363 | aliguori | sum = (sum & 0xFFFF)+(sum >> 16); |
42 | 48c64363 | aliguori | return ~sum;
|
43 | 48c64363 | aliguori | } |
44 | 48c64363 | aliguori | |
45 | 48c64363 | aliguori | uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, |
46 | 48c64363 | aliguori | uint8_t *addrs, uint8_t *buf) |
47 | 48c64363 | aliguori | { |
48 | 48c64363 | aliguori | uint32_t sum = 0;
|
49 | 48c64363 | aliguori | |
50 | 48c64363 | aliguori | sum += net_checksum_add(length, buf); // payload
|
51 | 48c64363 | aliguori | sum += net_checksum_add(8, addrs); // src + dst address |
52 | 48c64363 | aliguori | sum += proto + length; // protocol & length
|
53 | 48c64363 | aliguori | return net_checksum_finish(sum);
|
54 | 48c64363 | aliguori | } |
55 | 48c64363 | aliguori | |
56 | 48c64363 | aliguori | void net_checksum_calculate(uint8_t *data, int length) |
57 | 48c64363 | aliguori | { |
58 | 48c64363 | aliguori | int hlen, plen, proto, csum_offset;
|
59 | 48c64363 | aliguori | uint16_t csum; |
60 | 48c64363 | aliguori | |
61 | 48c64363 | aliguori | if ((data[14] & 0xf0) != 0x40) |
62 | 48c64363 | aliguori | return; /* not IPv4 */ |
63 | 48c64363 | aliguori | hlen = (data[14] & 0x0f) * 4; |
64 | 48c64363 | aliguori | plen = (data[16] << 8 | data[17]) - hlen; |
65 | 48c64363 | aliguori | proto = data[23];
|
66 | 48c64363 | aliguori | |
67 | 48c64363 | aliguori | switch (proto) {
|
68 | 48c64363 | aliguori | case PROTO_TCP:
|
69 | 48c64363 | aliguori | csum_offset = 16;
|
70 | 48c64363 | aliguori | break;
|
71 | 48c64363 | aliguori | case PROTO_UDP:
|
72 | 48c64363 | aliguori | csum_offset = 6;
|
73 | 48c64363 | aliguori | break;
|
74 | 48c64363 | aliguori | default:
|
75 | 48c64363 | aliguori | return;
|
76 | 48c64363 | aliguori | } |
77 | 48c64363 | aliguori | |
78 | 48c64363 | aliguori | if (plen < csum_offset+2) |
79 | 48c64363 | aliguori | return;
|
80 | 48c64363 | aliguori | |
81 | 48c64363 | aliguori | data[14+hlen+csum_offset] = 0; |
82 | 48c64363 | aliguori | data[14+hlen+csum_offset+1] = 0; |
83 | 48c64363 | aliguori | csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen); |
84 | 48c64363 | aliguori | data[14+hlen+csum_offset] = csum >> 8; |
85 | 48c64363 | aliguori | data[14+hlen+csum_offset+1] = csum & 0xff; |
86 | 48c64363 | aliguori | } |