blob: a4dc9b859c64d6c456f8651af9c6654a4efe6ef0 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
2 * Copyright 2011 Daniel Drown
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * checksum.c - ipv4/ipv6 checksum calculation
17 */
18#include <netinet/in.h>
19#include <netinet/ip.h>
20#include <netinet/ip_icmp.h>
21#include <netinet/udp.h>
22#include <netinet/tcp.h>
23#include <netinet/ip6.h>
24#include <netinet/icmp6.h>
25#include <linux/icmp.h>
26
27#include "checksum.h"
28
29/* function: ip_checksum_add
30 * adds data to a checksum
Lorenzo Colitti02786272013-04-08 18:02:24 +090031 * current - the current checksum (or 0 to start a new checksum)
Daniel Drowna45056e2012-03-23 10:42:54 -050032 * data - the data to add to the checksum
33 * len - length of data
34 */
Lorenzo Colitti02786272013-04-08 18:02:24 +090035uint32_t ip_checksum_add(uint32_t current, const void *data, int len) {
36 uint32_t checksum = current;
Daniel Drowna45056e2012-03-23 10:42:54 -050037 int left = len;
38 const uint16_t *data_16 = data;
39
40 while(left > 1) {
41 checksum += *data_16;
42 data_16++;
43 left -= 2;
44 }
45 if(left) {
46 checksum += *(uint8_t *)data_16;
47 }
48
49 return checksum;
50}
51
52/* function: ip_checksum_finish
53 * close the checksum
54 * temp_sum - sum from ip_checksum_add
55 */
56uint16_t ip_checksum_finish(uint32_t temp_sum) {
57 while(temp_sum > 0xffff)
58 temp_sum = (temp_sum >> 16) + (temp_sum & 0xFFFF);
59
60 temp_sum = (~temp_sum) & 0xffff;
61
62 return temp_sum;
63}
64
65/* function: ip_checksum
66 * combined ip_checksum_add and ip_checksum_finish
67 * data - data to checksum
68 * len - length of data
69 */
70uint16_t ip_checksum(const void *data, int len) {
71 uint32_t temp_sum;
72
73 temp_sum = ip_checksum_add(0,data,len);
74 return ip_checksum_finish(temp_sum);
75}
76
77/* function: ipv6_pseudo_header_checksum
78 * calculate the pseudo header checksum for use in tcp/udp/icmp headers
Lorenzo Colitti02786272013-04-08 18:02:24 +090079 * current - the current checksum or 0 to start a new checksum
80 * ip6 - the ipv6 header
81 * len - the transport length (transport header + payload)
Daniel Drowna45056e2012-03-23 10:42:54 -050082 */
Lorenzo Colitti02786272013-04-08 18:02:24 +090083uint32_t ipv6_pseudo_header_checksum(uint32_t current, const struct ip6_hdr *ip6, uint16_t len) {
Daniel Drowna45056e2012-03-23 10:42:54 -050084 uint32_t checksum_len, checksum_next;
85
Lorenzo Colitti02786272013-04-08 18:02:24 +090086 checksum_len = htonl((uint32_t) len);
Daniel Drowna45056e2012-03-23 10:42:54 -050087 checksum_next = htonl(ip6->ip6_nxt);
88
Lorenzo Colitti02786272013-04-08 18:02:24 +090089 current = ip_checksum_add(current, &(ip6->ip6_src), sizeof(struct in6_addr));
90 current = ip_checksum_add(current, &(ip6->ip6_dst), sizeof(struct in6_addr));
91 current = ip_checksum_add(current, &checksum_len, sizeof(checksum_len));
92 current = ip_checksum_add(current, &checksum_next, sizeof(checksum_next));
Daniel Drowna45056e2012-03-23 10:42:54 -050093
Lorenzo Colitti02786272013-04-08 18:02:24 +090094 return current;
Daniel Drowna45056e2012-03-23 10:42:54 -050095}
96
97/* function: ipv4_pseudo_header_checksum
98 * calculate the pseudo header checksum for use in tcp/udp headers
Lorenzo Colitti02786272013-04-08 18:02:24 +090099 * current - the current checksum or 0 to start a new checksum
100 * ip - the ipv4 header
101 * len - the transport length (transport header + payload)
Daniel Drowna45056e2012-03-23 10:42:54 -0500102 */
Lorenzo Colitti02786272013-04-08 18:02:24 +0900103uint32_t ipv4_pseudo_header_checksum(uint32_t current, const struct iphdr *ip, uint16_t len) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500104 uint16_t temp_protocol, temp_length;
105
106 temp_protocol = htons(ip->protocol);
Lorenzo Colitti02786272013-04-08 18:02:24 +0900107 temp_length = htons(len);
Daniel Drowna45056e2012-03-23 10:42:54 -0500108
Lorenzo Colitti02786272013-04-08 18:02:24 +0900109 current = ip_checksum_add(current, &(ip->saddr), sizeof(uint32_t));
110 current = ip_checksum_add(current, &(ip->daddr), sizeof(uint32_t));
111 current = ip_checksum_add(current, &temp_protocol, sizeof(uint16_t));
112 current = ip_checksum_add(current, &temp_length, sizeof(uint16_t));
Daniel Drowna45056e2012-03-23 10:42:54 -0500113
Lorenzo Colitti02786272013-04-08 18:02:24 +0900114 return current;
Daniel Drowna45056e2012-03-23 10:42:54 -0500115}