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/icmp6.h>
19 #include <netinet/in.h>
20 #include <netinet/ip.h>
21 #include <netinet/ip6.h>
22 #include <netinet/ip_icmp.h>
23 #include <netinet/tcp.h>
24 #include <netinet/udp.h>
25 
26 #include "checksum.h"
27 
28 /* function: ip_checksum_add
29  * adds data to a checksum. only known to work on little-endian hosts
30  * current - the current checksum (or 0 to start a new checksum)
31  *   data        - the data to add to the checksum
32  *   len         - length of data
33  */
ip_checksum_add(uint32_t current,const void * data,int len)34 uint32_t ip_checksum_add(uint32_t current, const void* data, int len) {
35     const uint16_t* data_16 = data;
36 
37     while (len >= 2) {
38         current += *data_16;
39         data_16++;
40         len -= 2;
41     }
42     if (len) current += *(uint8_t*)data_16;  // assumes little endian!
43 
44     return current;
45 }
46 
47 /* function: ip_checksum_fold
48  * folds a 32-bit partial checksum into 16 bits
49  *   temp_sum - sum from ip_checksum_add
50  *   returns: the folded checksum in network byte order
51  */
ip_checksum_fold(uint32_t temp_sum)52 uint16_t ip_checksum_fold(uint32_t temp_sum) {
53     temp_sum = (temp_sum >> 16) + (temp_sum & 0xFFFF);
54     temp_sum = (temp_sum >> 16) + (temp_sum & 0xFFFF);
55     return temp_sum;
56 }
57 
58 /* function: ip_checksum_finish
59  * folds and closes the checksum
60  *   temp_sum - sum from ip_checksum_add
61  *   returns: a header checksum value in network byte order
62  */
ip_checksum_finish(uint32_t temp_sum)63 uint16_t ip_checksum_finish(uint32_t temp_sum) {
64     return ~ip_checksum_fold(temp_sum);
65 }
66 
67 /* function: ip_checksum
68  * combined ip_checksum_add and ip_checksum_finish
69  *   data - data to checksum
70  *   len  - length of data
71  */
ip_checksum(const void * data,int len)72 uint16_t ip_checksum(const void* data, int len) {
73     return ip_checksum_finish(ip_checksum_add(0xFFFF, data, len));
74 }
75 
76 /* function: ipv6_pseudo_header_checksum
77  * calculate the pseudo header checksum for use in tcp/udp/icmp headers
78  *   ip6      - the ipv6 header
79  *   len      - the transport length (transport header + payload)
80  *   protocol - the transport layer protocol, can be different from ip6->ip6_nxt for fragments
81  */
ipv6_pseudo_header_checksum(const struct ip6_hdr * ip6,uint32_t len,uint8_t protocol)82 uint32_t ipv6_pseudo_header_checksum(const struct ip6_hdr* ip6, uint32_t len, uint8_t protocol) {
83     uint32_t checksum_len = htonl(len);
84     uint32_t checksum_next = htonl(protocol);
85     uint32_t current = 0;
86 
87     current = ip_checksum_add(current, &(ip6->ip6_src), sizeof(struct in6_addr));
88     current = ip_checksum_add(current, &(ip6->ip6_dst), sizeof(struct in6_addr));
89     current = ip_checksum_add(current, &checksum_len, sizeof(checksum_len));
90     current = ip_checksum_add(current, &checksum_next, sizeof(checksum_next));
91 
92     return current;
93 }
94 
95 /* function: ipv4_pseudo_header_checksum
96  * calculate the pseudo header checksum for use in tcp/udp headers
97  *   ip      - the ipv4 header
98  *   len     - the transport length (transport header + payload)
99  */
ipv4_pseudo_header_checksum(const struct iphdr * ip,uint16_t len)100 uint32_t ipv4_pseudo_header_checksum(const struct iphdr* ip, uint16_t len) {
101     uint16_t temp_protocol = htons(ip->protocol);
102     uint16_t temp_length = htons(len);
103     uint32_t current = 0;
104 
105     current = ip_checksum_add(current, &(ip->saddr), sizeof(uint32_t));
106     current = ip_checksum_add(current, &(ip->daddr), sizeof(uint32_t));
107     current = ip_checksum_add(current, &temp_protocol, sizeof(uint16_t));
108     current = ip_checksum_add(current, &temp_length, sizeof(uint16_t));
109 
110     return current;
111 }
112 
113 /* function: ip_checksum_adjust
114  * calculates a new checksum given a previous checksum and the old and new pseudo-header checksums
115  *   checksum    - the header checksum in the original packet in network byte order
116  *   old_hdr_sum - the pseudo-header checksum of the original packet
117  *   new_hdr_sum - the pseudo-header checksum of the translated packet
118  *   returns: the new header checksum in network byte order
119  */
ip_checksum_adjust(uint16_t checksum,uint32_t old_hdr_sum,uint32_t new_hdr_sum)120 uint16_t ip_checksum_adjust(uint16_t checksum, uint32_t old_hdr_sum, uint32_t new_hdr_sum) {
121     // Algorithm suggested in RFC 1624.
122     // http://tools.ietf.org/html/rfc1624#section-3
123     checksum = ~checksum;
124     uint16_t folded_sum = ip_checksum_fold(new_hdr_sum + checksum);
125     uint16_t folded_old = ip_checksum_fold(old_hdr_sum);
126     if (folded_sum > folded_old) {
127         return ~(folded_sum - folded_old);
128     } else {
129         return ~(folded_sum - folded_old - 1);  // end-around borrow
130     }
131 }
132