aboutgitcodebugslistschat
path: root/checksum.c
diff options
context:
space:
mode:
Diffstat (limited to 'checksum.c')
-rw-r--r--checksum.c137
1 files changed, 71 insertions, 66 deletions
diff --git a/checksum.c b/checksum.c
index f8a7b53..0894eca 100644
--- a/checksum.c
+++ b/checksum.c
@@ -59,6 +59,7 @@
#include "util.h"
#include "ip.h"
#include "checksum.h"
+#include "iov.h"
/* Checksums are optional for UDP over IPv4, so we usually just set
* them to 0. Change this to 1 to calculate real UDP over IPv4
@@ -84,7 +85,7 @@
*/
/* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */
__attribute__((optimize("-fno-strict-aliasing")))
-uint32_t sum_16b(const void *buf, size_t len)
+static uint32_t sum_16b(const void *buf, size_t len)
{
const uint16_t *p = buf;
uint32_t sum = 0;
@@ -106,7 +107,7 @@ uint32_t sum_16b(const void *buf, size_t len)
*
* Return: 16-bit folded sum
*/
-uint16_t csum_fold(uint32_t sum)
+static uint16_t csum_fold(uint32_t sum)
{
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
@@ -116,19 +117,19 @@ uint16_t csum_fold(uint32_t sum)
/**
* csum_ip4_header() - Calculate IPv4 header checksum
- * @tot_len: IPv4 payload length (data + IP header, network order)
- * @protocol: Protocol number (network order)
- * @saddr: IPv4 source address (network order)
- * @daddr: IPv4 destination address (network order)
+ * @l3len: IPv4 packet length (host order)
+ * @protocol: Protocol number
+ * @saddr: IPv4 source address
+ * @daddr: IPv4 destination address
*
* Return: 16-bit folded sum of the IPv4 header
*/
-uint16_t csum_ip4_header(uint16_t tot_len, uint8_t protocol,
+uint16_t csum_ip4_header(uint16_t l3len, uint8_t protocol,
struct in_addr saddr, struct in_addr daddr)
{
uint32_t sum = L2_BUF_IP4_PSUM(protocol);
- sum += tot_len;
+ sum += htons(l3len);
sum += (saddr.s_addr >> 16) & 0xffff;
sum += saddr.s_addr & 0xffff;
sum += (daddr.s_addr >> 16) & 0xffff;
@@ -140,13 +141,13 @@ uint16_t csum_ip4_header(uint16_t tot_len, uint8_t protocol,
/**
* proto_ipv4_header_psum() - Calculates the partial checksum of an
* IPv4 header for UDP or TCP
- * @tot_len: IPv4 Payload length (host order)
- * @proto: Protocol number (host order)
- * @saddr: Source address (network order)
- * @daddr: Destination address (network order)
+ * @l4len: IPv4 Payload length (host order)
+ * @proto: Protocol number
+ * @saddr: Source address
+ * @daddr: Destination address
* Returns: Partial checksum of the IPv4 header
*/
-uint32_t proto_ipv4_header_psum(uint16_t tot_len, uint8_t protocol,
+uint32_t proto_ipv4_header_psum(uint16_t l4len, uint8_t protocol,
struct in_addr saddr, struct in_addr daddr)
{
uint32_t psum = htons(protocol);
@@ -155,32 +156,47 @@ uint32_t proto_ipv4_header_psum(uint16_t tot_len, uint8_t protocol,
psum += saddr.s_addr & 0xffff;
psum += (daddr.s_addr >> 16) & 0xffff;
psum += daddr.s_addr & 0xffff;
- psum += htons(tot_len);
+ psum += htons(l4len);
return psum;
}
/**
+ * csum() - Compute TCP/IP-style checksum
+ * @buf: Input buffer
+ * @len: Input length
+ * @init: Initial 32-bit checksum, 0 for no pre-computed checksum
+ *
+ * Return: 16-bit folded, complemented checksum
+ */
+/* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */
+__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */
+static uint16_t csum(const void *buf, size_t len, uint32_t init)
+{
+ return (uint16_t)~csum_fold(csum_unfolded(buf, len, init));
+}
+
+/**
* csum_udp4() - Calculate and set checksum for a UDP over IPv4 packet
* @udp4hr: UDP header, initialised apart from checksum
* @saddr: IPv4 source address
* @daddr: IPv4 destination address
- * @payload: ICMPv4 packet payload
- * @len: Length of @payload (not including UDP)
+ * @data: UDP payload (as IO vector tail)
*/
void csum_udp4(struct udphdr *udp4hr,
struct in_addr saddr, struct in_addr daddr,
- const void *payload, size_t len)
+ struct iov_tail *data)
{
/* UDP checksums are optional, so don't bother */
udp4hr->check = 0;
if (UDP4_REAL_CHECKSUMS) {
- uint16_t tot_len = len + sizeof(struct udphdr);
- uint32_t psum = proto_ipv4_header_psum(tot_len, IPPROTO_UDP,
+ uint16_t l4len = iov_tail_size(data) + sizeof(struct udphdr);
+ uint32_t psum = proto_ipv4_header_psum(l4len, IPPROTO_UDP,
saddr, daddr);
+
psum = csum_unfolded(udp4hr, sizeof(struct udphdr), psum);
- udp4hr->check = csum(payload, len, psum);
+ udp4hr->check = csum_iov_tail(data, psum);
}
}
@@ -188,9 +204,9 @@ void csum_udp4(struct udphdr *udp4hr,
* csum_icmp4() - Calculate and set checksum for an ICMP packet
* @icmp4hr: ICMP header, initialised apart from checksum
* @payload: ICMP packet payload
- * @len: Length of @payload (not including ICMP header)
+ * @dlen: Length of @payload (not including ICMP header)
*/
-void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t len)
+void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t dlen)
{
uint32_t psum;
@@ -199,16 +215,16 @@ void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t len)
/* Partial checksum for ICMP header alone */
psum = sum_16b(icmp4hr, sizeof(*icmp4hr));
- icmp4hr->checksum = csum(payload, len, psum);
+ icmp4hr->checksum = csum(payload, dlen, psum);
}
/**
* proto_ipv6_header_psum() - Calculates the partial checksum of an
* IPv6 header for UDP or TCP
* @payload_len: IPv6 payload length (host order)
- * @proto: Protocol number (host order)
- * @saddr: Source address (network order)
- * @daddr: Destination address (network order)
+ * @proto: Protocol number
+ * @saddr: Source address
+ * @daddr: Destination address
* Returns: Partial checksum of the IPv6 header
*/
uint32_t proto_ipv6_header_psum(uint16_t payload_len, uint8_t protocol,
@@ -226,19 +242,22 @@ uint32_t proto_ipv6_header_psum(uint16_t payload_len, uint8_t protocol,
/**
* csum_udp6() - Calculate and set checksum for a UDP over IPv6 packet
* @udp6hr: UDP header, initialised apart from checksum
- * @payload: UDP packet payload
- * @len: Length of @payload (not including UDP header)
+ * @saddr: Source address
+ * @daddr: Destination address
+ * @data: UDP payload (as IO vector tail)
*/
void csum_udp6(struct udphdr *udp6hr,
const struct in6_addr *saddr, const struct in6_addr *daddr,
- const void *payload, size_t len)
+ struct iov_tail *data)
{
- uint32_t psum = proto_ipv6_header_psum(len + sizeof(struct udphdr),
- IPPROTO_UDP, saddr, daddr);
+ uint16_t l4len = iov_tail_size(data) + sizeof(struct udphdr);
+ uint32_t psum = proto_ipv6_header_psum(l4len, IPPROTO_UDP,
+ saddr, daddr);
+
udp6hr->check = 0;
psum = csum_unfolded(udp6hr, sizeof(struct udphdr), psum);
- udp6hr->check = csum(payload, len, psum);
+ udp6hr->check = csum_iov_tail(data, psum);
}
/**
@@ -247,21 +266,19 @@ void csum_udp6(struct udphdr *udp6hr,
* @saddr: IPv6 source address
* @daddr: IPv6 destination address
* @payload: ICMP packet payload
- * @len: Length of @payload (not including ICMPv6 header)
+ * @dlen: Length of @payload (not including ICMPv6 header)
*/
void csum_icmp6(struct icmp6hdr *icmp6hr,
const struct in6_addr *saddr, const struct in6_addr *daddr,
- const void *payload, size_t len)
+ const void *payload, size_t dlen)
{
- /* Partial checksum for the pseudo-IPv6 header */
- uint32_t psum = sum_16b(saddr, sizeof(*saddr)) +
- sum_16b(daddr, sizeof(*daddr)) +
- htons(len + sizeof(*icmp6hr)) + htons(IPPROTO_ICMPV6);
+ uint32_t psum = proto_ipv6_header_psum(dlen + sizeof(*icmp6hr),
+ IPPROTO_ICMPV6, saddr, daddr);
icmp6hr->icmp6_cksum = 0;
/* Add in partial checksum for the ICMPv6 header alone */
psum += sum_16b(icmp6hr, sizeof(*icmp6hr));
- icmp6hr->icmp6_cksum = csum(payload, len, psum);
+ icmp6hr->icmp6_cksum = csum(payload, dlen, psum);
}
#ifdef __AVX2__
@@ -450,7 +467,8 @@ uint32_t csum_unfolded(const void *buf, size_t len, uint32_t init)
intptr_t align = ROUND_UP((intptr_t)buf, sizeof(__m256i));
unsigned int pad = align - (intptr_t)buf;
- if (len < pad)
+ /* Don't mix sum_16b() and csum_avx2() with odd padding lengths */
+ if (pad & 1 || len < pad)
pad = len;
if (pad)
@@ -480,36 +498,23 @@ uint32_t csum_unfolded(const void *buf, size_t len, uint32_t init)
#endif /* !__AVX2__ */
/**
- * csum() - Compute TCP/IP-style checksum
- * @buf: Input buffer
- * @len: Input length
- * @init: Initial 32-bit checksum, 0 for no pre-computed checksum
- *
- * Return: 16-bit folded, complemented checksum
- */
-/* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */
-__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */
-uint16_t csum(const void *buf, size_t len, uint32_t init)
-{
- return (uint16_t)~csum_fold(csum_unfolded(buf, len, init));
-}
-
-/**
- * csum_iov() - Calculates the unfolded checksum over an array of IO vectors
- *
- * @iov Pointer to the array of IO vectors
- * @n Length of the array
+ * csum_iov_tail() - Calculate unfolded checksum for the tail of an IO vector
+ * @tail: IO vector tail to checksum
* @init Initial 32-bit checksum, 0 for no pre-computed checksum
*
* Return: 16-bit folded, complemented checksum
*/
-/* cppcheck-suppress unusedFunction */
-uint16_t csum_iov(const struct iovec *iov, size_t n, uint32_t init)
+uint16_t csum_iov_tail(struct iov_tail *tail, uint32_t init)
{
- unsigned int i;
-
- for (i = 0; i < n; i++)
- init = csum_unfolded(iov[i].iov_base, iov[i].iov_len, init);
-
+ if (iov_tail_prune(tail)) {
+ size_t i;
+
+ init = csum_unfolded((char *)tail->iov[0].iov_base + tail->off,
+ tail->iov[0].iov_len - tail->off, init);
+ for (i = 1; i < tail->cnt; i++) {
+ const struct iovec *iov = &tail->iov[i];
+ init = csum_unfolded(iov->iov_base, iov->iov_len, init);
+ }
+ }
return (uint16_t)~csum_fold(init);
}