1
0
mirror of https://passt.top/passt synced 2024-07-01 23:42:41 +00:00

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

View File

@ -6,11 +6,13 @@
#ifndef CHECKSUM_H
#define CHECKSUM_H
struct icmphdr;
struct icmp6hdr;
uint32_t sum_16b(const void *buf, size_t len);
uint16_t csum_fold(uint32_t sum);
uint16_t csum_unaligned(const void *buf, size_t len, uint32_t init);
void csum_icmp4(struct icmphdr *ih, const void *payload, size_t len);
void csum_icmp6(struct icmp6hdr *icmp6hr,
const struct in6_addr *saddr, const struct in6_addr *daddr,
const void *payload, size_t len);

4
tap.c
View File

@ -148,9 +148,7 @@ void tap_ip_send(const struct ctx *c, const struct in6_addr *src, uint8_t proto,
uh->check = 0;
} else if (iph->protocol == IPPROTO_ICMP) {
struct icmphdr *ih = (struct icmphdr *)(iph + 1);
ih->checksum = 0;
ih->checksum = csum_unaligned(ih, len, 0);
csum_icmp4(ih, ih + 1, len - sizeof(*ih));
}
if (tap_send(c, buf, len + sizeof(*iph) + sizeof(*eh), 1) < 0)