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 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 "net/checksum.h"
23
uint32_t net_checksum_add(int len, uint8_t *buf)
28
for (i = 0; i < len; i++) {
30
sum += (uint32_t)buf[i];
32
sum += (uint32_t)buf[i] << 8;
37
uint16_t net_checksum_finish(uint32_t sum)
40
sum = (sum & 0xFFFF)+(sum >> 16);
44
uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
45
uint8_t *addrs, uint8_t *buf)
49
sum += net_checksum_add(length, buf); // payload
50
sum += net_checksum_add(8, addrs); // src + dst address
51
sum += proto + length; // protocol & length
52
return net_checksum_finish(sum);
55
void net_checksum_calculate(uint8_t *data, int length)
57
int hlen, plen, proto, csum_offset;
60
if ((data[14] & 0xf0) != 0x40)
61
return; /* not IPv4 */
62
hlen = (data[14] & 0x0f) * 4;
63
plen = (data[16] << 8 | data[17]) - hlen;
77
if (plen < csum_offset+2)
80
data[14+hlen+csum_offset] = 0;
81
data[14+hlen+csum_offset+1] = 0;
82
csum = net_checksum_tcpudp(plen, proto, data+14+12, data+14+hlen);
83
data[14+hlen+csum_offset] = csum >> 8;
84
data[14+hlen+csum_offset+1] = csum & 0xff;