summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2020-02-07 12:18:46 +0000
committerRoy Marples <roy@marples.name>2020-02-07 12:20:27 +0000
commitd3c3fe30d6acffdc7cc59aa7bcc51952b35071ad (patch)
treeef524124dc21ac52dbf2dcf57347bfe7f90548d0
parent4f2e7d1065a1a431bdab85b809e0c9a77f1347a3 (diff)
downloaddhcpcd-d3c3fe30d6acffdc7cc59aa7bcc51952b35071ad.tar.xz
inet6: Calculate the prefix in the canonical form
Rather than being clever and getting it wrong.
-rw-r--r--src/ipv6.c23
1 files changed, 6 insertions, 17 deletions
diff --git a/src/ipv6.c b/src/ipv6.c
index e09c3376..c81aac86 100644
--- a/src/ipv6.c
+++ b/src/ipv6.c
@@ -380,25 +380,14 @@ ipv6_makeaddr(struct in6_addr *addr, struct interface *ifp,
static int
ipv6_makeprefix(struct in6_addr *prefix, const struct in6_addr *addr, int len)
{
- int bytes, bits;
+ struct in6_addr mask;
+ size_t i;
- if (len < 0 || len > 128) {
- errno = EINVAL;
+ if (ipv6_mask(&mask, len) == -1)
return -1;
- }
-
- bytes = len / NBBY;
- bits = len % NBBY;
- memcpy(&prefix->s6_addr, &addr->s6_addr, (size_t)bytes);
- if (bits != 0) {
- /* Coverify false positive.
- * bytelen cannot be 16 if bitlen is non zero */
- /* coverity[overrun-local] */
- prefix->s6_addr[bytes] =
- (uint8_t)(prefix->s6_addr[bytes] >> (NBBY - bits));
- }
- memset((char *)prefix->s6_addr + bytes, 0,
- sizeof(prefix->s6_addr) - (size_t)bytes);
+ *prefix = *addr;
+ for (i = 0; i < sizeof(prefix->s6_addr); i++)
+ prefix->s6_addr[i] &= mask.s6_addr[i];
return 0;
}