blob: 11b51493a3d368727f5eb0190b0ea915f564f39a [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 */
junyulaic4e591a2018-11-26 22:36:10 +090018#include <netinet/icmp6.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050019#include <netinet/in.h>
20#include <netinet/ip.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050021#include <netinet/ip6.h>
junyulaic4e591a2018-11-26 22:36:10 +090022#include <netinet/ip_icmp.h>
23#include <netinet/tcp.h>
24#include <netinet/udp.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050025
26#include "checksum.h"
27
28/* function: ip_checksum_add
29 * adds data to a checksum
Lorenzo Colitti02786272013-04-08 18:02:24 +090030 * current - the current checksum (or 0 to start a new checksum)
junyulaic4e591a2018-11-26 22:36:10 +090031 * data - the data to add to the checksum
32 * len - length of data
Daniel Drowna45056e2012-03-23 10:42:54 -050033 */
Lorenzo Colitti02786272013-04-08 18:02:24 +090034uint32_t ip_checksum_add(uint32_t current, const void *data, int len) {
junyulaic4e591a2018-11-26 22:36:10 +090035 uint32_t checksum = current;
36 int left = len;
Daniel Drowna45056e2012-03-23 10:42:54 -050037 const uint16_t *data_16 = data;
38
junyulaic4e591a2018-11-26 22:36:10 +090039 while (left > 1) {
Daniel Drowna45056e2012-03-23 10:42:54 -050040 checksum += *data_16;
41 data_16++;
42 left -= 2;
43 }
junyulaic4e591a2018-11-26 22:36:10 +090044 if (left) {
Daniel Drowna45056e2012-03-23 10:42:54 -050045 checksum += *(uint8_t *)data_16;
46 }
47
48 return checksum;
49}
50
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090051/* function: ip_checksum_fold
52 * folds a 32-bit partial checksum into 16 bits
junyulaic4e591a2018-11-26 22:36:10 +090053 * temp_sum - sum from ip_checksum_add
54 * returns: the folded checksum in network byte order
Daniel Drowna45056e2012-03-23 10:42:54 -050055 */
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090056uint16_t ip_checksum_fold(uint32_t temp_sum) {
junyulaic4e591a2018-11-26 22:36:10 +090057 while (temp_sum > 0xffff) {
Daniel Drowna45056e2012-03-23 10:42:54 -050058 temp_sum = (temp_sum >> 16) + (temp_sum & 0xFFFF);
junyulaic4e591a2018-11-26 22:36:10 +090059 }
Daniel Drowna45056e2012-03-23 10:42:54 -050060 return temp_sum;
61}
62
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090063/* function: ip_checksum_finish
64 * folds and closes the checksum
junyulaic4e591a2018-11-26 22:36:10 +090065 * temp_sum - sum from ip_checksum_add
66 * returns: a header checksum value in network byte order
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090067 */
junyulaic4e591a2018-11-26 22:36:10 +090068uint16_t ip_checksum_finish(uint32_t temp_sum) { return ~ip_checksum_fold(temp_sum); }
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090069
Daniel Drowna45056e2012-03-23 10:42:54 -050070/* function: ip_checksum
71 * combined ip_checksum_add and ip_checksum_finish
junyulaic4e591a2018-11-26 22:36:10 +090072 * data - data to checksum
73 * len - length of data
Daniel Drowna45056e2012-03-23 10:42:54 -050074 */
75uint16_t ip_checksum(const void *data, int len) {
76 uint32_t temp_sum;
77
junyulaic4e591a2018-11-26 22:36:10 +090078 temp_sum = ip_checksum_add(0, data, len);
Daniel Drowna45056e2012-03-23 10:42:54 -050079 return ip_checksum_finish(temp_sum);
80}
81
82/* function: ipv6_pseudo_header_checksum
83 * calculate the pseudo header checksum for use in tcp/udp/icmp headers
junyulaic4e591a2018-11-26 22:36:10 +090084 * ip6 - the ipv6 header
85 * len - the transport length (transport header + payload)
86 * protocol - the transport layer protocol, can be different from ip6->ip6_nxt for fragments
Daniel Drowna45056e2012-03-23 10:42:54 -050087 */
Lorenzo Colitti07f02652014-02-20 14:28:43 +090088uint32_t ipv6_pseudo_header_checksum(const struct ip6_hdr *ip6, uint16_t len, uint8_t protocol) {
Daniel Drowna45056e2012-03-23 10:42:54 -050089 uint32_t checksum_len, checksum_next;
junyulaic4e591a2018-11-26 22:36:10 +090090 checksum_len = htonl((uint32_t)len);
Lorenzo Colitti07f02652014-02-20 14:28:43 +090091 checksum_next = htonl(protocol);
Daniel Drowna45056e2012-03-23 10:42:54 -050092
Lorenzo Colitti07f02652014-02-20 14:28:43 +090093 uint32_t current = 0;
junyulaic4e591a2018-11-26 22:36:10 +090094
Lorenzo Colitti02786272013-04-08 18:02:24 +090095 current = ip_checksum_add(current, &(ip6->ip6_src), sizeof(struct in6_addr));
96 current = ip_checksum_add(current, &(ip6->ip6_dst), sizeof(struct in6_addr));
97 current = ip_checksum_add(current, &checksum_len, sizeof(checksum_len));
98 current = ip_checksum_add(current, &checksum_next, sizeof(checksum_next));
Daniel Drowna45056e2012-03-23 10:42:54 -050099
Lorenzo Colitti02786272013-04-08 18:02:24 +0900100 return current;
Daniel Drowna45056e2012-03-23 10:42:54 -0500101}
102
103/* function: ipv4_pseudo_header_checksum
104 * calculate the pseudo header checksum for use in tcp/udp headers
junyulaic4e591a2018-11-26 22:36:10 +0900105 * ip - the ipv4 header
106 * len - the transport length (transport header + payload)
Daniel Drowna45056e2012-03-23 10:42:54 -0500107 */
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900108uint32_t ipv4_pseudo_header_checksum(const struct iphdr *ip, uint16_t len) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500109 uint16_t temp_protocol, temp_length;
110
111 temp_protocol = htons(ip->protocol);
junyulaic4e591a2018-11-26 22:36:10 +0900112 temp_length = htons(len);
Daniel Drowna45056e2012-03-23 10:42:54 -0500113
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900114 uint32_t current = 0;
junyulaic4e591a2018-11-26 22:36:10 +0900115
Lorenzo Colitti02786272013-04-08 18:02:24 +0900116 current = ip_checksum_add(current, &(ip->saddr), sizeof(uint32_t));
117 current = ip_checksum_add(current, &(ip->daddr), sizeof(uint32_t));
118 current = ip_checksum_add(current, &temp_protocol, sizeof(uint16_t));
119 current = ip_checksum_add(current, &temp_length, sizeof(uint16_t));
Daniel Drowna45056e2012-03-23 10:42:54 -0500120
Lorenzo Colitti02786272013-04-08 18:02:24 +0900121 return current;
Daniel Drowna45056e2012-03-23 10:42:54 -0500122}
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900123
124/* function: ip_checksum_adjust
125 * calculates a new checksum given a previous checksum and the old and new pseudo-header checksums
junyulaic4e591a2018-11-26 22:36:10 +0900126 * checksum - the header checksum in the original packet in network byte order
127 * old_hdr_sum - the pseudo-header checksum of the original packet
128 * new_hdr_sum - the pseudo-header checksum of the translated packet
129 * returns: the new header checksum in network byte order
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900130 */
131uint16_t ip_checksum_adjust(uint16_t checksum, uint32_t old_hdr_sum, uint32_t new_hdr_sum) {
132 // Algorithm suggested in RFC 1624.
133 // http://tools.ietf.org/html/rfc1624#section-3
junyulaic4e591a2018-11-26 22:36:10 +0900134 checksum = ~checksum;
Lorenzo Colitti5a50c022014-02-10 09:20:05 +0900135 uint16_t folded_sum = ip_checksum_fold(checksum + new_hdr_sum);
136 uint16_t folded_old = ip_checksum_fold(old_hdr_sum);
137 if (folded_sum > folded_old) {
138 return ~(folded_sum - folded_old);
139 } else {
140 return ~(folded_sum - folded_old - 1); // end-around borrow
141 }
142}