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 authored and wolfy1339 committed Mar 3, 2024
1 parent 5a65a48 commit 2b885a0
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 @@ -265,12 +265,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 @@ -292,10 +298,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 @@ -321,12 +328,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 @@ -348,10 +361,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 2b885a0

Please sign in to comment.