Skip to content

Commit

Permalink
Fix bug where less specific subnet may be selected
Browse files Browse the repository at this point in the history
  • Loading branch information
iczero committed Sep 16, 2021
1 parent b3081eb commit 94f8c4b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/subnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,18 @@ subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
// Search all subnets for a matching one

int last_rtt = INT_MAX; // current smallest rtt seen
int last_prefix_length = -1; // most specific prefix length seen

for splay_each(subnet_t, p, &subnet_tree) {
if(!p || p->type != SUBNET_IPV4) {
continue;
}

// we are on a route less specific than the one we found, break
if(p->net.ipv4.prefixlength < last_prefix_length) {
break;
}

if(!maskcmp(address, &p->net.ipv4.address, p->net.ipv4.prefixlength)) {
if(!p->owner) {
// this is a broadcast subnet
Expand All @@ -291,10 +297,11 @@ subnet_t *lookup_subnet_ipv4(const ipv4_t *address) {
if(rtt < last_rtt) {
r = p;
last_rtt = rtt;
last_prefix_length = p->net.ipv4.prefixlength;
}
}
} else if(r) {
// no more matching subnets
// no more matching subnets with equal or greater specificity
break;
}
}
Expand All @@ -320,12 +327,18 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
// Search all subnets for a matching one

int last_rtt = INT_MAX; // current smallest rtt seen
int last_prefix_length = -1; // most specific prefix length seen

for splay_each(subnet_t, p, &subnet_tree) {
if(!p || p->type != SUBNET_IPV6) {
continue;
}

// we are on a route less specific than the one we found, break
if(p->net.ipv6.prefixlength < last_prefix_length) {
break;
}

if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength)) {
if(!p->owner) {
// this is a broadcast subnet
Expand All @@ -347,10 +360,11 @@ subnet_t *lookup_subnet_ipv6(const ipv6_t *address) {
if(rtt < last_rtt) {
r = p;
last_rtt = rtt;
last_prefix_length = p->net.ipv6.prefixlength;
}
}
} else if(r) {
// no more matching subnets
// no more matching subnets with equal or greater specificity
break;
}
}
Expand Down

0 comments on commit 94f8c4b

Please sign in to comment.