diff options
-rw-r--r-- | checksum.c | 16 | ||||
-rw-r--r-- | checksum.h | 2 | ||||
-rw-r--r-- | tap.c | 4 |
3 files changed, 19 insertions, 3 deletions
@@ -52,6 +52,7 @@ #include <stddef.h> #include <stdint.h> +#include <linux/icmp.h> #include <linux/icmpv6.h> /** @@ -108,6 +109,21 @@ uint16_t csum_unaligned(const void *buf, size_t len, uint32_t 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 * @saddr: IPv6 source address @@ -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); @@ -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) |