From bb63eed1830e8243646efa4630efed9ba245f61b Mon Sep 17 00:00:00 2001 From: leo chung Date: Fri, 18 Jun 2021 16:23:34 +0800 Subject: [PATCH] napt: fix checksum of UDP According to the UDP protocol, the checksum is optional. so if the checksum is 0, this meaning the checksum is turn off, and no need to correct checksum. Signed-off-by: leo chung --- src/core/ipv4/ip4_napt.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/core/ipv4/ip4_napt.c b/src/core/ipv4/ip4_napt.c index a2272632..16229ac4 100644 --- a/src/core/ipv4/ip4_napt.c +++ b/src/core/ipv4/ip4_napt.c @@ -567,10 +567,12 @@ static void ip_napt_modify_port_udp(struct udp_hdr *udphdr, u8_t dest, u16_t newval) { if (dest) { - checksumadjust((u8_t *)&udphdr->chksum, (u8_t *)&udphdr->dest, 2, (u8_t *)&newval, 2); + if (udphdr->chksum != 0) + checksumadjust((u8_t *)&udphdr->chksum, (u8_t *)&udphdr->dest, 2, (u8_t *)&newval, 2); udphdr->dest = newval; } else { - checksumadjust((u8_t *)&udphdr->chksum, (u8_t *)&udphdr->src, 2, (u8_t *)&newval, 2); + if (udphdr->chksum != 0) + checksumadjust((u8_t *)&udphdr->chksum, (u8_t *)&udphdr->src, 2, (u8_t *)&newval, 2); udphdr->src = newval; } } @@ -578,7 +580,8 @@ ip_napt_modify_port_udp(struct udp_hdr *udphdr, u8_t dest, u16_t newval) static void ip_napt_modify_addr_udp(struct udp_hdr *udphdr, ip4_addr_p_t *oldval, u32_t newval) { - checksumadjust( (u8_t *)&udphdr->chksum, (u8_t *)&oldval->addr, 4, (u8_t *)&newval, 4); + if (udphdr->chksum != 0) + checksumadjust( (u8_t *)&udphdr->chksum, (u8_t *)&oldval->addr, 4, (u8_t *)&newval, 4); } #endif /* LWIP_UDP */