blob: 4b23897302ddaed8ca58ff3942109206b4367ce8 [file] [log] [blame]
Harald Welted46bcd22017-08-08 23:27:22 +02001/*
2 *
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * IP/TCP/UDP checksumming routines
8 *
9 * Authors: Jorge Cwik, <jorge@laser.satlink.net>
10 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
11 * Tom May, <ftom@netcom.com>
12 * Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
13 * Lots of code moved from tcp.c and ip.c; see those files
14 * for more names.
15 *
16 * 03/02/96 Jes Sorensen, Andreas Schwab, Roman Hodek:
17 * Fixed some nasty bugs, causing some horrible crashes.
18 * A: At some points, the sum (%0) was used as
19 * length-counter instead of the length counter
20 * (%1). Thanks to Roman Hodek for pointing this out.
21 * B: GCC seems to mess up if one uses too many
22 * data-registers to hold input values and one tries to
23 * specify d0 and d1 as scratch registers. Letting gcc
24 * choose these registers itself solves the problem.
25 *
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * as published by the Free Software Foundation; either version
29 * 2 of the License, or (at your option) any later version.
30 */
31
32/* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access
33 kills, so most of the assembly has to go. */
34
35#if defined(__FreeBSD__)
36#define _KERNEL /* needed on FreeBSD 10.x for s6_addr32 */
37#include <sys/types.h>
38#include <netinet/in.h>
39#include <sys/endian.h>
40#endif
41
42#include "checksum.h"
43#include <arpa/inet.h>
44
45static inline unsigned short from32to16(unsigned int x)
46{
47 /* add up 16-bit and 16-bit for 16+c bit */
48 x = (x & 0xffff) + (x >> 16);
49 /* add up carry.. */
50 x = (x & 0xffff) + (x >> 16);
51 return x;
52}
53
54static unsigned int do_csum(const unsigned char *buff, int len)
55{
56 int odd;
57 unsigned int result = 0;
58
59 if (len <= 0)
60 goto out;
61 odd = 1 & (unsigned long) buff;
62 if (odd) {
63#if BYTE_ORDER == LITTLE_ENDIAN
64 result += (*buff << 8);
65#else
66 result = *buff;
67#endif
68 len--;
69 buff++;
70 }
71 if (len >= 2) {
72 if (2 & (unsigned long) buff) {
73 result += *(unsigned short *) buff;
74 len -= 2;
75 buff += 2;
76 }
77 if (len >= 4) {
78 const unsigned char *end = buff + ((unsigned)len & ~3);
79 unsigned int carry = 0;
80 do {
81 unsigned int w = *(unsigned int *) buff;
82 buff += 4;
83 result += carry;
84 result += w;
85 carry = (w > result);
86 } while (buff < end);
87 result += carry;
88 result = (result & 0xffff) + (result >> 16);
89 }
90 if (len & 2) {
91 result += *(unsigned short *) buff;
92 buff += 2;
93 }
94 }
95 if (len & 1)
96#if BYTE_ORDER == LITTLE_ENDIAN
97 result += *buff;
98#else
99 result += (*buff << 8);
100#endif
101 result = from32to16(result);
102 if (odd)
103 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
104out:
105 return result;
106}
107
108/*
109 * This is a version of ip_compute_csum() optimized for IP headers,
110 * which always checksum on 4 octet boundaries.
111 */
112uint16_t ip_fast_csum(const void *iph, unsigned int ihl)
113{
114 return (uint16_t)~do_csum(iph, ihl*4);
115}
116
117/*
118 * computes the checksum of a memory block at buff, length len,
119 * and adds in "sum" (32-bit)
120 *
121 * returns a 32-bit number suitable for feeding into itself
122 * or csum_tcpudp_magic
123 *
124 * this function must be called with even lengths, except
125 * for the last fragment, which may be odd
126 *
127 * it's best to have buff aligned on a 32-bit boundary
128 */
129uint32_t csum_partial(const void *buff, int len, uint32_t wsum)
130{
131 unsigned int sum = (unsigned int)wsum;
132 unsigned int result = do_csum(buff, len);
133
134 /* add in old sum, and carry.. */
135 result += sum;
136 if (sum > result)
137 result += 1;
138 return (uint32_t)result;
139}
140
141/*
142 * this routine is used for miscellaneous IP-like checksums, mainly
143 * in icmp.c
144 */
145uint16_t ip_compute_csum(const void *buff, int len)
146{
147 return (uint16_t)~do_csum(buff, len);
148}
149
150uint16_t csum_ipv6_magic(const struct in6_addr *saddr,
151 const struct in6_addr *daddr,
152 uint32_t len, uint8_t proto, uint32_t csum)
153{
154 int carry;
155 uint32_t ulen;
156 uint32_t uproto;
157 uint32_t sum = (uint32_t)csum;
158
159 sum += (uint32_t)saddr->s6_addr32[0];
160 carry = (sum < (uint32_t)saddr->s6_addr32[0]);
161 sum += carry;
162
163 sum += (uint32_t)saddr->s6_addr32[1];
164 carry = (sum < (uint32_t)saddr->s6_addr32[1]);
165 sum += carry;
166
167 sum += (uint32_t)saddr->s6_addr32[2];
168 carry = (sum < (uint32_t)saddr->s6_addr32[2]);
169 sum += carry;
170
171 sum += (uint32_t)saddr->s6_addr32[3];
172 carry = (sum < (uint32_t)saddr->s6_addr32[3]);
173 sum += carry;
174
175 sum += (uint32_t)daddr->s6_addr32[0];
176 carry = (sum < (uint32_t)daddr->s6_addr32[0]);
177 sum += carry;
178
179 sum += (uint32_t)daddr->s6_addr32[1];
180 carry = (sum < (uint32_t)daddr->s6_addr32[1]);
181 sum += carry;
182
183 sum += (uint32_t)daddr->s6_addr32[2];
184 carry = (sum < (uint32_t)daddr->s6_addr32[2]);
185 sum += carry;
186
187 sum += (uint32_t)daddr->s6_addr32[3];
188 carry = (sum < (uint32_t)daddr->s6_addr32[3]);
189 sum += carry;
190
191 ulen = (uint32_t)htonl((uint32_t) len);
192 sum += ulen;
193 carry = (sum < ulen);
194 sum += carry;
195
196 uproto = (uint32_t)htonl(proto);
197 sum += uproto;
198 carry = (sum < uproto);
199 sum += carry;
200
201 return csum_fold((uint32_t)sum);
202}
203
204/* fold a partial checksum */
205uint16_t csum_fold(uint32_t csum)
206{
207 uint32_t sum = (uint32_t)csum;
208 sum = (sum & 0xffff) + (sum >> 16);
209 sum = (sum & 0xffff) + (sum >> 16);
210 return (uint16_t)~sum;
211}