summaryrefslogtreecommitdiff
path: root/sys/lib/libsa/in_cksum.c
diff options
context:
space:
mode:
authorPatrick Wildt <patrick@cvs.openbsd.org>2020-05-18 17:01:03 +0000
committerPatrick Wildt <patrick@cvs.openbsd.org>2020-05-18 17:01:03 +0000
commitc958650fef6067ca1c0470eed943686df37542bf (patch)
treef3ce85d7ad33b527363a951d7b64eff8f374352a /sys/lib/libsa/in_cksum.c
parent4e4fe16e3bc59be6357c3399794e13a98aebaf0d (diff)
Sync in_cksum.c to the same version ospfd has. This fixes problems
with odd packet lengths, which can happen when using TFTP to load a file with an odd length. ospfd actually took dvmrpd's version in 2006 to fix the same issue, and both daemons implementations are the same. For the bootloader we keep the consts from the previous version and replace the fatal with a print and return. ok deraadt@
Diffstat (limited to 'sys/lib/libsa/in_cksum.c')
-rw-r--r--sys/lib/libsa/in_cksum.c55
1 files changed, 27 insertions, 28 deletions
diff --git a/sys/lib/libsa/in_cksum.c b/sys/lib/libsa/in_cksum.c
index d3f2e6ac978..c6d4ccdd4ae 100644
--- a/sys/lib/libsa/in_cksum.c
+++ b/sys/lib/libsa/in_cksum.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: in_cksum.c,v 1.5 2014/11/19 20:28:56 miod Exp $ */
+/* $OpenBSD: in_cksum.c,v 1.6 2020/05/18 17:01:02 patrick Exp $ */
/* $NetBSD: in_cksum.c,v 1.3 1995/04/22 13:53:48 cgd Exp $ */
/*
@@ -17,11 +17,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Lawrence Berkeley Laboratory and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
@@ -56,35 +52,38 @@
* code and should be modified for each CPU to be as fast as possible.
* In particular, it should not be this one.
*/
-int
-in_cksum(const void *p, int len)
+u_int16_t
+in_cksum(const void *p, size_t l)
{
- int sum = 0, oddbyte = 0, v = 0;
+ unsigned int sum = 0;
+ int len;
const u_char *cp = p;
- /* we assume < 2^16 bytes being summed */
- while (len > 0) {
- if (oddbyte) {
- sum += v + *cp++;
- len--;
+ /* ensure that < 2^16 bytes being summed */
+ if (l >= (1 << 16)) {
+ printf("in_cksum: packet too big\n");
+ return -1;
+ }
+ len = (int)l;
+
+ if (((long)cp & 1) == 0) {
+ while (len > 1) {
+ sum += htons(*(const u_short *)cp);
+ cp += 2;
+ len -= 2;
}
- if (((long)cp & 1) == 0) {
- while ((len -= 2) >= 0) {
- sum += *(const u_short *)cp;
- cp += 2;
- }
- } else {
- while ((len -= 2) >= 0) {
- sum += *cp++ << 8;
- sum += *cp++;
- }
+ } else {
+ while (len > 1) {
+ sum += *cp++ << 8;
+ sum += *cp++;
+ len -= 2;
}
- if ((oddbyte = len & 1) != 0)
- v = *cp << 8;
}
- if (oddbyte)
- sum += v;
+ if (len == 1)
+ sum += *cp << 8;
+
sum = (sum >> 16) + (sum & 0xffff); /* add in accumulated carries */
sum += sum >> 16; /* add potential last carry */
+ sum = ntohs(sum);
return (0xffff & ~sum);
}