checksum.c revision a45056e35c1af2a0f0a6eed258fd5fdf4846a79f
1/*
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
31 * current_sum - the current checksum (or 0 to start a new checksum)
32 * data        - the data to add to the checksum
33 * len         - length of data
34 */
35uint32_t ip_checksum_add(uint32_t current_sum, const void *data, int len) {
36  uint32_t checksum = current_sum;
37  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
79 * current_sum - the current checksum or 0 to start a new checksum
80 * ip6         - the ipv6 header
81 */
82uint32_t ipv6_pseudo_header_checksum(uint32_t current_sum, const struct ip6_hdr *ip6) {
83  uint32_t checksum_len, checksum_next;
84
85  checksum_len = htonl(ntohs(ip6->ip6_plen));
86  checksum_next = htonl(ip6->ip6_nxt);
87
88  current_sum = ip_checksum_add(current_sum,&(ip6->ip6_src),sizeof(struct in6_addr));
89  current_sum = ip_checksum_add(current_sum,&(ip6->ip6_dst),sizeof(struct in6_addr));
90  current_sum = ip_checksum_add(current_sum,&checksum_len,sizeof(checksum_len));
91  current_sum = ip_checksum_add(current_sum,&checksum_next,sizeof(checksum_next));
92
93  return current_sum;
94}
95
96/* function: ipv4_pseudo_header_checksum
97 * calculate the pseudo header checksum for use in tcp/udp headers
98 * current_sum - the current checksum or 0 to start a new checksum
99 * ip          - the ipv4 header
100 */
101uint32_t ipv4_pseudo_header_checksum(uint32_t current_sum, const struct iphdr *ip) {
102  uint16_t temp_protocol, temp_length;
103
104  temp_protocol = htons(ip->protocol);
105  temp_length = htons(ntohs(ip->tot_len) - ip->ihl*4);
106
107  current_sum = ip_checksum_add(current_sum, &(ip->saddr), sizeof(uint32_t));
108  current_sum = ip_checksum_add(current_sum, &(ip->daddr), sizeof(uint32_t));
109  current_sum = ip_checksum_add(current_sum, &temp_protocol, sizeof(uint16_t));
110  current_sum = ip_checksum_add(current_sum, &temp_length, sizeof(uint16_t));
111
112  return current_sum;
113}
114