summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpatrick <patrick@openbsd.org>2020-05-18 17:01:02 +0000
committerpatrick <patrick@openbsd.org>2020-05-18 17:01:02 +0000
commit419596d7e7d9396d7ff0a802278e9a2db9b17f1e (patch)
tree3c58be5e2ff43f6869043f38f8ee0950699528db
parentMove boot.mac into the Attic. (diff)
downloadwireguard-openbsd-419596d7e7d9396d7ff0a802278e9a2db9b17f1e.tar.xz
wireguard-openbsd-419596d7e7d9396d7ff0a802278e9a2db9b17f1e.zip
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@
-rw-r--r--sys/lib/libsa/in_cksum.c55
-rw-r--r--sys/lib/libsa/net.h4
2 files changed, 29 insertions, 30 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);
}
diff --git a/sys/lib/libsa/net.h b/sys/lib/libsa/net.h
index 48e21dadf17..c809d02791b 100644
--- a/sys/lib/libsa/net.h
+++ b/sys/lib/libsa/net.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: net.h,v 1.10 2014/11/19 20:28:56 miod Exp $ */
+/* $OpenBSD: net.h,v 1.11 2020/05/18 17:01:02 patrick Exp $ */
/* $NetBSD: net.h,v 1.10 1995/10/20 00:46:30 cgd Exp $ */
/*
@@ -115,7 +115,7 @@ ssize_t sendrecv(struct iodesc *,
/* Utilities: */
const char *ether_sprintf(const u_char *);
-int in_cksum(const void *, int);
+u_int16_t in_cksum(const void *, size_t);
const char *inet_ntoa(struct in_addr);
const char *intoa(u_int32_t); /* similar to inet_ntoa */
u_int32_t inet_addr(const char *);