]> git.ipfire.org Git - location/libloc.git/commitdiff
Validate the prefix to be within range
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 2 Jan 2018 17:17:57 +0000 (17:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 2 Jan 2018 17:17:57 +0000 (17:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/network.c

index dc1775a83da18424b231547a64dabf887703620d..866b377ba08f1bc6f9f50715edad49a238e89fd1 100644 (file)
@@ -37,6 +37,18 @@ struct loc_network {
        uint32_t asn;
 };
 
+static int valid_prefix(struct in6_addr* address, unsigned int prefix) {
+       // The prefix cannot be larger than 128 bits
+       if (prefix > 128)
+               return 1;
+
+       // And the prefix cannot be zero
+       if (prefix == 0)
+               return 1;
+
+       return 0;
+}
+
 LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network,
                struct in6_addr start_address, unsigned int prefix) {
        // Address cannot be unspecified
@@ -63,6 +75,12 @@ LOC_EXPORT int loc_network_new(struct loc_ctx* ctx, struct loc_network** network
                return -EINVAL;
        }
 
+       // Validate the prefix
+       if (valid_prefix(&start_address, prefix) != 0) {
+               DEBUG(ctx, "Invalid prefix: %u\n", prefix);
+               return -EINVAL;
+       }
+
        struct loc_network* n = calloc(1, sizeof(*n));
        if (!n)
                return -ENOMEM;