Use typing to reduce chances of IPv4 endianness errors
We recently corrected some errors handling the endianness of IPv4 addresses. These are very easy errors to make since although we mostly store them in network endianness, we sometimes need to manipulate them in host endianness. To reduce the chances of making such mistakes again, change to always using a (struct in_addr) instead of a bare in_addr_t or uint32_t to store network endian addresses. This makes it harder to accidentally do arithmetic or comparisons on such addresses as if they were host endian. We introduce a number of IN4_IS_ADDR_*() helpers to make it easier to directly work with struct in_addr values. This has the additional benefit of making the IPv4 and IPv6 paths more visually similar. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:

committed by
Stefano Brivio

parent
dd3470d9a9
commit
7c7b68dbe0
@@ -133,7 +133,8 @@ void csum_ip4_header(struct iphdr *ip4h)
|
||||
* @payload: ICMPv4 packet payload
|
||||
* @len: Length of @payload (not including UDP)
|
||||
*/
|
||||
void csum_udp4(struct udphdr *udp4hr, in_addr_t saddr, in_addr_t daddr,
|
||||
void csum_udp4(struct udphdr *udp4hr,
|
||||
struct in_addr saddr, struct in_addr daddr,
|
||||
const void *payload, size_t len)
|
||||
{
|
||||
/* UDP checksums are optional, so don't bother */
|
||||
@@ -142,8 +143,8 @@ void csum_udp4(struct udphdr *udp4hr, in_addr_t saddr, in_addr_t daddr,
|
||||
if (UDP4_REAL_CHECKSUMS) {
|
||||
/* UNTESTED: if we did want real UDPv4 checksums, this
|
||||
* is roughly what we'd need */
|
||||
uint32_t psum = csum_fold(htonl(saddr))
|
||||
+ csum_fold(htonl(daddr))
|
||||
uint32_t psum = csum_fold(saddr.s_addr)
|
||||
+ csum_fold(daddr.s_addr)
|
||||
+ htons(len + sizeof(*udp4hr))
|
||||
+ htons(IPPROTO_UDP);
|
||||
/* Add in partial checksum for the UDP header alone */
|
||||
|
Reference in New Issue
Block a user