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.
7 * IP/TCP/UDP checksumming routines
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
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.
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.
32 /* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access
33 kills, so most of the assembly has to go. */
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>
43 #include <arpa/inet.h>
45 static inline unsigned short from32to16(unsigned int x
)
47 /* add up 16-bit and 16-bit for 16+c bit */
48 x
= (x
& 0xffff) + (x
>> 16);
50 x
= (x
& 0xffff) + (x
>> 16);
54 static unsigned int do_csum(const unsigned char *buff
, int len
)
57 unsigned int result
= 0;
61 odd
= 1 & (unsigned long) buff
;
63 #if BYTE_ORDER == LITTLE_ENDIAN
64 result
+= (*buff
<< 8);
72 if (2 & (unsigned long) buff
) {
73 result
+= *(unsigned short *) buff
;
78 const unsigned char *end
= buff
+ ((unsigned)len
& ~3);
79 unsigned int carry
= 0;
81 unsigned int w
= *(unsigned int *) buff
;
88 result
= (result
& 0xffff) + (result
>> 16);
91 result
+= *(unsigned short *) buff
;
96 #if BYTE_ORDER == LITTLE_ENDIAN
99 result
+= (*buff
<< 8);
101 result
= from32to16(result
);
103 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
109 * This is a version of ip_compute_csum() optimized for IP headers,
110 * which always checksum on 4 octet boundaries.
112 uint16_t ip_fast_csum(const void *iph
, unsigned int ihl
)
114 return (uint16_t)~do_csum(iph
, ihl
*4);
118 * computes the checksum of a memory block at buff, length len,
119 * and adds in "sum" (32-bit)
121 * returns a 32-bit number suitable for feeding into itself
122 * or csum_tcpudp_magic
124 * this function must be called with even lengths, except
125 * for the last fragment, which may be odd
127 * it's best to have buff aligned on a 32-bit boundary
129 uint32_t csum_partial(const void *buff
, int len
, uint32_t wsum
)
131 unsigned int sum
= (unsigned int)wsum
;
132 unsigned int result
= do_csum(buff
, len
);
134 /* add in old sum, and carry.. */
138 return (uint32_t)result
;
142 * this routine is used for miscellaneous IP-like checksums, mainly
145 uint16_t ip_compute_csum(const void *buff
, int len
)
147 return (uint16_t)~do_csum(buff
, len
);
150 uint16_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
)
157 uint32_t sum
= (uint32_t)csum
;
159 sum
+= (uint32_t)saddr
->s6_addr32
[0];
160 carry
= (sum
< (uint32_t)saddr
->s6_addr32
[0]);
163 sum
+= (uint32_t)saddr
->s6_addr32
[1];
164 carry
= (sum
< (uint32_t)saddr
->s6_addr32
[1]);
167 sum
+= (uint32_t)saddr
->s6_addr32
[2];
168 carry
= (sum
< (uint32_t)saddr
->s6_addr32
[2]);
171 sum
+= (uint32_t)saddr
->s6_addr32
[3];
172 carry
= (sum
< (uint32_t)saddr
->s6_addr32
[3]);
175 sum
+= (uint32_t)daddr
->s6_addr32
[0];
176 carry
= (sum
< (uint32_t)daddr
->s6_addr32
[0]);
179 sum
+= (uint32_t)daddr
->s6_addr32
[1];
180 carry
= (sum
< (uint32_t)daddr
->s6_addr32
[1]);
183 sum
+= (uint32_t)daddr
->s6_addr32
[2];
184 carry
= (sum
< (uint32_t)daddr
->s6_addr32
[2]);
187 sum
+= (uint32_t)daddr
->s6_addr32
[3];
188 carry
= (sum
< (uint32_t)daddr
->s6_addr32
[3]);
191 ulen
= (uint32_t)htonl((uint32_t) len
);
193 carry
= (sum
< ulen
);
196 uproto
= (uint32_t)htonl(proto
);
198 carry
= (sum
< uproto
);
201 return csum_fold((uint32_t)sum
);
204 /* fold a partial checksum */
205 uint16_t csum_fold(uint32_t csum
)
207 uint32_t sum
= (uint32_t)csum
;
208 sum
= (sum
& 0xffff) + (sum
>> 16);
209 sum
= (sum
& 0xffff) + (sum
>> 16);
210 return (uint16_t)~sum
;