checksum.c revision 0278627f576832860af2d84e04e383ecaa92d74f
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 - 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, const void *data, int len) {
36  uint32_t checksum = current;
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 - the current checksum or 0 to start a new checksum
80 * ip6     - the ipv6 header
81 * len     - the transport length (transport header + payload)
82 */
83uint32_t ipv6_pseudo_header_checksum(uint32_t current, const struct ip6_hdr *ip6, uint16_t len) {
84  uint32_t checksum_len, checksum_next;
85
86  checksum_len = htonl((uint32_t) len);
87  checksum_next = htonl(ip6->ip6_nxt);
88
89  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));
93
94  return current;
95}
96
97/* function: ipv4_pseudo_header_checksum
98 * calculate the pseudo header checksum for use in tcp/udp headers
99 * current - the current checksum or 0 to start a new checksum
100 * ip      - the ipv4 header
101 * len     - the transport length (transport header + payload)
102 */
103uint32_t ipv4_pseudo_header_checksum(uint32_t current, const struct iphdr *ip, uint16_t len) {
104  uint16_t temp_protocol, temp_length;
105
106  temp_protocol = htons(ip->protocol);
107  temp_length = htons(len);
108
109  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));
113
114  return current;
115}
116