Add csum_icmp4() helper for calculating ICMP checksums

Although tap_ip_send() is currently the only place calculating ICMP
checksums, create a helper function for symmetry with ICMPv6.  For
future flexibility it allows the ICMPv6 header and payload to be in
separate buffers.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson
2022-10-19 11:43:45 +11:00
committed by Stefano Brivio
parent 7abd2b0d72
commit 67ab617172
3 changed files with 19 additions and 3 deletions

View File

@@ -52,6 +52,7 @@
#include <stddef.h>
#include <stdint.h>
#include <linux/icmp.h>
#include <linux/icmpv6.h>
/**
@@ -107,6 +108,21 @@ uint16_t csum_unaligned(const void *buf, size_t len, uint32_t init)
return (uint16_t)~csum_fold(sum_16b(buf, len) + init);
}
/**
* 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)
*/
void csum_icmp4(struct icmphdr *icmp4hr, const void *payload, size_t len)
{
/* Partial checksum for ICMP header alone */
uint32_t psum = sum_16b(icmp4hr, sizeof(*icmp4hr));
icmp4hr->checksum = 0;
icmp4hr->checksum = csum_unaligned(payload, len, psum);
}
/**
* csum_icmp6() - Calculate and set checksum for an ICMPv6 packet
* @icmp6hr: ICMPv6 header, initialised apart from checksum