diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2022-11-04 14:10:35 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2022-11-04 12:04:24 +0100 |
commit | 7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf (patch) | |
tree | 2cf45b6d9019e785f3c1c20bfee84576c46501a4 /tap.c | |
parent | dd3470d9a92bd2fc83b3afd5ff9490b73de6a58c (diff) | |
download | passt-7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf.tar passt-7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf.tar.gz passt-7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf.tar.bz2 passt-7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf.tar.lz passt-7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf.tar.xz passt-7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf.tar.zst passt-7c7b68dbe02874324e4fcda9c13b9e8d9a8192cf.zip |
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>
Diffstat (limited to 'tap.c')
-rw-r--r-- | tap.c | 18 |
1 files changed, 9 insertions, 9 deletions
@@ -92,7 +92,7 @@ int tap_send(const struct ctx *c, const void *data, size_t len) * * Returns: IPv4 address, network order */ -in_addr_t tap_ip4_daddr(const struct ctx *c) +struct in_addr tap_ip4_daddr(const struct ctx *c) { return c->ip4.addr_seen; } @@ -141,7 +141,7 @@ static void *tap_push_l2h(const struct ctx *c, void *buf, uint16_t proto) * * Return: pointer at which to write the packet's payload */ -static void *tap_push_ip4h(char *buf, in_addr_t src, in_addr_t dst, +static void *tap_push_ip4h(char *buf, struct in_addr src, struct in_addr dst, size_t len, uint8_t proto) { struct iphdr *ip4h = (struct iphdr *)buf; @@ -154,8 +154,8 @@ static void *tap_push_ip4h(char *buf, in_addr_t src, in_addr_t dst, ip4h->frag_off = 0; ip4h->ttl = 255; ip4h->protocol = proto; - ip4h->saddr = src; - ip4h->daddr = dst; + ip4h->saddr = src.s_addr; + ip4h->daddr = dst.s_addr; csum_ip4_header(ip4h); return ip4h + 1; } @@ -170,8 +170,8 @@ static void *tap_push_ip4h(char *buf, in_addr_t src, in_addr_t dst, * @in: UDP payload contents (not including UDP header) * @len: UDP payload length (not including UDP header) */ -void tap_udp4_send(const struct ctx *c, in_addr_t src, in_port_t sport, - in_addr_t dst, in_port_t dport, +void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport, + struct in_addr dst, in_port_t dport, const void *in, size_t len) { size_t udplen = len + sizeof(struct udphdr); @@ -199,7 +199,7 @@ void tap_udp4_send(const struct ctx *c, in_addr_t src, in_port_t sport, * @in: ICMP packet, including ICMP header * @len: ICMP packet length, including ICMP header */ -void tap_icmp4_send(const struct ctx *c, in_addr_t src, in_addr_t dst, +void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst, void *in, size_t len) { char buf[USHRT_MAX]; @@ -448,8 +448,8 @@ resume: l4_len = l3_len - hlen; - if (iph->saddr && c->ip4.addr_seen != iph->saddr) { - c->ip4.addr_seen = iph->saddr; + if (iph->saddr && c->ip4.addr_seen.s_addr != iph->saddr) { + c->ip4.addr_seen.s_addr = iph->saddr; proto_update_l2_buf(NULL, NULL, &c->ip4.addr_seen); } |