Use IPV4_IS_LOOPBACK more widely

This macro checks if an IPv4 address is in the loopback network
(127.0.0.0/8).  There are two places where we open code an identical check,
use the macro instead.

There are also a number of places we specifically exclude the loopback
address (127.0.0.1), but we should actually be excluding anything in the
loopback network.  Change those sites to use the macro as well.

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-11-04 14:10:34 +11:00 committed by Stefano Brivio
parent dd09cceaee
commit dd3470d9a9
2 changed files with 5 additions and 5 deletions

8
conf.c
View file

@ -389,7 +389,7 @@ static void get_dns(struct ctx *c)
dns4 - &c->ip4.dns[0] < ARRAY_SIZE(c->ip4.dns) - 1 && dns4 - &c->ip4.dns[0] < ARRAY_SIZE(c->ip4.dns) - 1 &&
inet_pton(AF_INET, p + 1, dns4)) { inet_pton(AF_INET, p + 1, dns4)) {
/* We can only access local addresses via the gw redirect */ /* We can only access local addresses via the gw redirect */
if (ntohl(*dns4) >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET) { if (IPV4_IS_LOOPBACK(ntohl(*dns4))) {
if (c->no_map_gw) { if (c->no_map_gw) {
*dns4 = 0; *dns4 = 0;
continue; continue;
@ -1190,7 +1190,7 @@ void conf(struct ctx *c, int argc, char **argv)
inet_pton(AF_INET, optarg, &c->ip4.dns_fwd) && inet_pton(AF_INET, optarg, &c->ip4.dns_fwd) &&
c->ip4.dns_fwd != htonl(INADDR_ANY) && c->ip4.dns_fwd != htonl(INADDR_ANY) &&
c->ip4.dns_fwd != htonl(INADDR_BROADCAST) && c->ip4.dns_fwd != htonl(INADDR_BROADCAST) &&
c->ip4.dns_fwd != htonl(INADDR_LOOPBACK)) !IPV4_IS_LOOPBACK(ntohl(c->ip4.dns_fwd)))
break; break;
err("Invalid DNS forwarding address: %s", optarg); err("Invalid DNS forwarding address: %s", optarg);
@ -1388,7 +1388,7 @@ void conf(struct ctx *c, int argc, char **argv)
inet_pton(AF_INET, optarg, &c->ip4.addr) && inet_pton(AF_INET, optarg, &c->ip4.addr) &&
c->ip4.addr != htonl(INADDR_ANY) && c->ip4.addr != htonl(INADDR_ANY) &&
c->ip4.addr != htonl(INADDR_BROADCAST) && c->ip4.addr != htonl(INADDR_BROADCAST) &&
c->ip4.addr != htonl(INADDR_LOOPBACK) && !IPV4_IS_LOOPBACK(ntohl(c->ip4.addr)) &&
!IN_MULTICAST(ntohl(c->ip4.addr))) !IN_MULTICAST(ntohl(c->ip4.addr)))
break; break;
@ -1424,7 +1424,7 @@ void conf(struct ctx *c, int argc, char **argv)
inet_pton(AF_INET, optarg, &c->ip4.gw) && inet_pton(AF_INET, optarg, &c->ip4.gw) &&
c->ip4.gw != htonl(INADDR_ANY) && c->ip4.gw != htonl(INADDR_ANY) &&
c->ip4.gw != htonl(INADDR_BROADCAST) && c->ip4.gw != htonl(INADDR_BROADCAST) &&
c->ip4.gw != htonl(INADDR_LOOPBACK)) !IPV4_IS_LOOPBACK(ntohl(c->ip4.gw)))
break; break;
err("Invalid gateway address: %s", optarg); err("Invalid gateway address: %s", optarg);

2
udp.c
View file

@ -680,7 +680,7 @@ static void udp_sock_fill_data_v4(const struct ctx *c, int n,
src = ntohl(b->s_in.sin_addr.s_addr); src = ntohl(b->s_in.sin_addr.s_addr);
src_port = ntohs(b->s_in.sin_port); src_port = ntohs(b->s_in.sin_port);
if (src >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET || if (IPV4_IS_LOOPBACK(src) ||
src == INADDR_ANY || src == ntohl(c->ip4.addr_seen)) { src == INADDR_ANY || src == ntohl(c->ip4.addr_seen)) {
b->iph.saddr = c->ip4.gw; b->iph.saddr = c->ip4.gw;
udp_tap_map[V4][src_port].ts = now->tv_sec; udp_tap_map[V4][src_port].ts = now->tv_sec;