2007-11-07 00:38:14 +01:00
|
|
|
/****************************************************************************
|
2014-06-27 23:42:21 +02:00
|
|
|
* net/utils/net_chksum.c
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-11-07 00:38:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
2014-06-27 23:42:21 +02:00
|
|
|
#include "utils/utils.h"
|
2007-11-07 00:38:14 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2023-10-18 08:11:44 +02:00
|
|
|
* Private Functions
|
2007-11-07 00:38:14 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
/****************************************************************************
|
2023-10-18 08:11:44 +02:00
|
|
|
* Name: checksum
|
2017-04-02 22:30:01 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2021-01-15 10:01:00 +01:00
|
|
|
* Calculate the raw change sum over the memory region described by
|
2017-04-02 22:30:01 +02:00
|
|
|
* data and len.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2019-03-11 19:48:17 +01:00
|
|
|
* sum - Partial calculations carried over from a previous call to
|
|
|
|
* chksum(). This should be zero on the first time that check
|
|
|
|
* sum is called.
|
2017-04-02 22:30:01 +02:00
|
|
|
* data - Beginning of the data to include in the checksum.
|
|
|
|
* len - Length of the data to include in the checksum.
|
2023-10-18 08:11:44 +02:00
|
|
|
* odd - the flag of the Calculated data sum
|
2017-04-02 22:30:01 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The updated checksum value.
|
|
|
|
*
|
2014-06-28 01:51:32 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-03 03:48:31 +02:00
|
|
|
#ifndef CONFIG_NET_ARCH_CHKSUM
|
2023-10-18 08:11:44 +02:00
|
|
|
uint16_t checksum(uint16_t sum, FAR const uint8_t *data,
|
|
|
|
uint16_t len, bool *odd)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
2015-01-20 18:06:51 +01:00
|
|
|
FAR const uint8_t *dataptr;
|
|
|
|
FAR const uint8_t *last_byte;
|
2009-12-15 15:53:45 +01:00
|
|
|
uint16_t t;
|
2007-11-07 00:38:14 +01:00
|
|
|
|
|
|
|
dataptr = data;
|
|
|
|
last_byte = data + len - 1;
|
|
|
|
|
2023-10-18 08:11:44 +02:00
|
|
|
if (*odd == true)
|
|
|
|
{
|
|
|
|
t = dataptr[0];
|
|
|
|
sum += t;
|
|
|
|
if (sum < t)
|
|
|
|
{
|
|
|
|
sum++; /* carry */
|
|
|
|
}
|
|
|
|
|
|
|
|
dataptr += 1;
|
|
|
|
}
|
|
|
|
|
2014-04-12 21:09:48 +02:00
|
|
|
while (dataptr < last_byte)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
|
|
|
/* At least two more bytes */
|
|
|
|
|
2015-01-20 18:06:51 +01:00
|
|
|
t = ((uint16_t)dataptr[0] << 8) + dataptr[1];
|
2007-11-07 00:38:14 +01:00
|
|
|
sum += t;
|
|
|
|
if (sum < t)
|
|
|
|
{
|
|
|
|
sum++; /* carry */
|
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
dataptr += 2;
|
|
|
|
}
|
|
|
|
|
2023-10-18 08:11:44 +02:00
|
|
|
*odd = false;
|
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
if (dataptr == last_byte)
|
|
|
|
{
|
|
|
|
t = (dataptr[0] << 8) + 0;
|
|
|
|
sum += t;
|
|
|
|
if (sum < t)
|
|
|
|
{
|
|
|
|
sum++; /* carry */
|
|
|
|
}
|
2023-10-18 08:11:44 +02:00
|
|
|
|
|
|
|
*odd = true;
|
2007-11-07 00:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return sum in host byte order. */
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
2023-10-18 08:11:44 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the raw change sum over the memory region described by
|
|
|
|
* data and len.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sum - Partial calculations carried over from a previous call to
|
|
|
|
* chksum(). This should be zero on the first time that check
|
|
|
|
* sum is called.
|
|
|
|
* data - Beginning of the data to include in the checksum.
|
|
|
|
* len - Length of the data to include in the checksum.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The updated checksum value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
uint16_t chksum(uint16_t sum, FAR const uint8_t *data, uint16_t len)
|
|
|
|
{
|
|
|
|
bool odd = false;
|
|
|
|
|
|
|
|
return checksum(sum, data, len, &odd);
|
|
|
|
}
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
#endif /* CONFIG_NET_ARCH_CHKSUM */
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: chksum_iob
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the Internet checksum over an iob chain buffer.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sum - Partial calculations carried over from a previous call to
|
|
|
|
* chksum(). This should be zero on the first time that check
|
|
|
|
* sum is called.
|
|
|
|
* iob - An iob chain buffer over which the checksum is to be computed.
|
|
|
|
* offset - Specifies the byte offset of the start of valid data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The updated checksum value.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MM_IOB
|
|
|
|
uint16_t chksum_iob(uint16_t sum, FAR struct iob_s *iob, uint16_t offset)
|
|
|
|
{
|
|
|
|
/* Skip to the I/O buffer containing the data offset */
|
|
|
|
|
2023-10-18 08:11:44 +02:00
|
|
|
bool odd = false;
|
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
while (iob != NULL && offset > iob->io_len)
|
|
|
|
{
|
|
|
|
offset -= iob->io_len;
|
|
|
|
iob = iob->io_flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the link pointer is not empty, loop to walk through all I/O buffer
|
|
|
|
* and accumulate the sum
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (iob != NULL)
|
|
|
|
{
|
2023-10-18 08:11:44 +02:00
|
|
|
sum = checksum(sum, iob->io_data + iob->io_offset + offset,
|
|
|
|
iob->io_len - offset, &odd);
|
2022-11-23 12:39:40 +01:00
|
|
|
iob = iob->io_flink;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MM_IOB */
|
|
|
|
|
2014-06-28 01:51:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_chksum
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the Internet checksum over a buffer.
|
|
|
|
*
|
|
|
|
* The Internet checksum is the one's complement of the one's complement
|
|
|
|
* sum of all 16-bit words in the buffer.
|
|
|
|
*
|
|
|
|
* See RFC1071.
|
|
|
|
*
|
|
|
|
* If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
|
|
|
|
* provided by architecture-specific logic.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* buf - A pointer to the buffer over which the checksum is to be computed.
|
|
|
|
*
|
2021-01-15 10:01:00 +01:00
|
|
|
* len - The length of the buffer over which the checksum is to be
|
|
|
|
* computed.
|
2014-06-28 01:51:32 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The Internet checksum of the buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-09-03 03:48:31 +02:00
|
|
|
#ifndef CONFIG_NET_ARCH_CHKSUM
|
2014-06-28 01:51:32 +02:00
|
|
|
uint16_t net_chksum(FAR uint16_t *data, uint16_t len)
|
2007-11-07 00:38:14 +01:00
|
|
|
{
|
2022-01-18 08:38:00 +01:00
|
|
|
return HTONS(chksum(0, (uint8_t *)data, len));
|
2007-11-07 00:38:14 +01:00
|
|
|
}
|
2014-06-28 01:51:32 +02:00
|
|
|
#endif /* CONFIG_NET_ARCH_CHKSUM */
|
2007-11-07 00:38:14 +01:00
|
|
|
|
2022-11-23 12:39:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_chksum_iob
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Calculate the Internet checksum over an iob chain buffer.
|
|
|
|
*
|
|
|
|
* The Internet checksum is the one's complement of the one's complement
|
|
|
|
* sum of all 16-bit words in the buffer.
|
|
|
|
*
|
|
|
|
* See RFC1071.
|
|
|
|
*
|
|
|
|
* If CONFIG_NET_ARCH_CHKSUM is defined, then this function must be
|
|
|
|
* provided by architecture-specific logic.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sum - Partial calculations carried over from a previous call to
|
|
|
|
* chksum(). This should be zero on the first time that check
|
|
|
|
* sum is called.
|
|
|
|
* iob - An iob chain buffer over which the checksum is to be computed.
|
|
|
|
* offset - Specifies the byte offset of the start of valid data.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The Internet checksum of the given iob chain buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MM_IOB
|
|
|
|
uint16_t net_chksum_iob(uint16_t sum, FAR struct iob_s *iob, uint16_t offset)
|
|
|
|
{
|
|
|
|
return HTONS(chksum_iob(sum, iob, offset));
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_MM_IOB */
|
|
|
|
|
2022-10-31 07:48:46 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_chksum_adjust
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Adjusts the checksum of a packet without having to completely
|
|
|
|
* recalculate it, as described in RFC 3022, Section 4.2, Page 9.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* chksum - points to the chksum in the packet
|
|
|
|
* optr - points to the old data in the packet
|
|
|
|
* olen - length of old data
|
|
|
|
* nptr - points to the new data in the packet
|
|
|
|
* nlen - length of new data
|
|
|
|
*
|
|
|
|
* Limitations:
|
|
|
|
* The algorithm is applicable only for even offsets and even lengths.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void net_chksum_adjust(FAR uint16_t *chksum,
|
|
|
|
FAR const uint16_t *optr, ssize_t olen,
|
|
|
|
FAR const uint16_t *nptr, ssize_t nlen)
|
|
|
|
{
|
|
|
|
int32_t x;
|
|
|
|
int32_t oldval;
|
|
|
|
int32_t newval;
|
|
|
|
|
|
|
|
x = NTOHS(*chksum);
|
|
|
|
x = ~x & 0xffff;
|
|
|
|
while (olen > 0)
|
|
|
|
{
|
|
|
|
oldval = NTOHS(*optr);
|
|
|
|
|
|
|
|
x -= oldval & 0xffff;
|
|
|
|
if (x <= 0)
|
|
|
|
{
|
|
|
|
x--;
|
|
|
|
x &= 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
optr++;
|
|
|
|
olen -= 2;
|
|
|
|
}
|
|
|
|
while (nlen > 0)
|
|
|
|
{
|
|
|
|
newval = NTOHS(*nptr);
|
|
|
|
|
|
|
|
x += newval & 0xffff;
|
|
|
|
if ((x & 0x10000) != 0)
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
x &= 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
nptr++;
|
|
|
|
nlen -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
x = ~x & 0xffff;
|
|
|
|
*chksum = HTONS(x);
|
|
|
|
}
|
|
|
|
|
2007-11-07 00:38:14 +01:00
|
|
|
#endif /* CONFIG_NET */
|