From dd09cceaee216afc90101ee5c3a2d57b1ca1a042 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Fri, 4 Nov 2022 14:10:33 +1100 Subject: Minor improvements to IPv4 netmask handling There are several minor problems with our parsing of IPv4 netmasks (-n). First, we don't reject nonsensical netmasks like 0.255.0.255. Address this structurally by using prefix length instead of netmask as the primary variable, only converting (and validating) when we need to. This has the added benefit of making some things more uniform with the IPv6 path. Second, when the user specifies a prefix length, we truncate the output from strtol() to an integer, which means we would treat -n 4294967320 as valid (equivalent to 24). Fix types to check for this. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- pasta.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'pasta.c') diff --git a/pasta.c b/pasta.c index ae695e2..db86317 100644 --- a/pasta.c +++ b/pasta.c @@ -249,19 +249,16 @@ void pasta_ns_conf(struct ctx *c) nl_link(1, 1 /* lo */, MAC_ZERO, 1, 0); if (c->pasta_conf_ns) { - int prefix_len; - nl_link(1, c->pasta_ifi, c->mac_guest, 1, c->mtu); if (c->ifi4) { - prefix_len = __builtin_popcount(c->ip4.mask); nl_addr(1, c->pasta_ifi, AF_INET, &c->ip4.addr, - &prefix_len, NULL); + &c->ip4.prefix_len, NULL); nl_route(1, c->pasta_ifi, AF_INET, &c->ip4.gw); } if (c->ifi6) { - prefix_len = 64; + int prefix_len = 64; nl_addr(1, c->pasta_ifi, AF_INET6, &c->ip6.addr, &prefix_len, NULL); nl_route(1, c->pasta_ifi, AF_INET6, &c->ip6.gw); -- cgit v1.2.3