Skip to content

Commit

Permalink
don't accept improper ipv6 netmasks
Browse files Browse the repository at this point in the history
  • Loading branch information
kjokjo committed Jun 10, 2017
1 parent 041a2fa commit 45098e7
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions ipcalc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ EOF
# get netmasks
if ($ipv6) {
if (defined $ARGV[1]) {
$mask1 = $ARGV[1];
$mask1 = check_mask6($ARGV[1]);
if ($mask1 == -1) {
print "INVALID MASK1\n\n";
exit;
}
} else {
$mask1 = 64;
}
Expand Down Expand Up @@ -199,6 +203,8 @@ EOF
print set_color($error_color);
}
print "$error\n";
print set_color($norml_color);
exit;
}

# print "Address: ".ntoa($address)."\n";
Expand Down Expand Up @@ -1089,7 +1095,7 @@ sub ip6ton
my $test = $arg;
$test =~ s/[0-9a-f:]//g;
if ($test ne '') {
print "Illegal chars\n";
###print "Illegal chars\n";
return -1;
}
$test = $arg;
Expand All @@ -1109,7 +1115,7 @@ print "Illegal chars\n";
#print "- '$slice'\n";
if ($slice eq '') {
if ($compressed) {
print "multiple ::\n";
###print "multiple ::\n";
return -1;
}
for (my $i=0;$i<8-$#tmp;$i++) {
Expand Down Expand Up @@ -1149,6 +1155,15 @@ sub prefixlenton
return $n;
}

sub check_mask6
{
my $mask = shift;
if ($mask !~ /^\d{1,3}$/ || $mask < 0 || $mask > 128) {
return -1;
}
return $mask;
}

sub argton
# expects 1. an address as dotted decimals, bit-count-mask, or hex
# 2. netmask flag. if set -> check netmask and negate wildcard
Expand Down Expand Up @@ -1188,25 +1203,26 @@ sub argton

# bit-count-mask (24 or /24)
$arg =~ s/^\/(\d+)$/$1/;
# ipv6
# for ipv6 this code is never reached:
if ($ipv6) {
if ($arg =~ /^\d{1,3}$/) {
if ($arg < 0 || $arg > 128) {
return -1;
}
return prefixlenton($arg);
if ($arg !~ /^\d{1,3}$/ || $arg < 0 || $arg > 128) {
return -1;
}
} else {
if ($arg =~ /^\d{1,2}$/) {
if ($arg < 0 || $arg > 32) {
return -1;
}
for ($i=0;$i<$arg;$i++) {
$n |= 1 << (31-$i);
}
return $n;
return prefixlenton($arg);
}
# ipv4
if ($arg =~ /^\d{1,2}$/) {
if ($arg < 0 || $arg > 32) {
return -1;
}
for ($i=0;$i<$arg;$i++) {
$n |= 1 << (31-$i);
}
return $n;
}


# hex
if ($arg =~ /^[0-9A-Fa-f]{8}$/ ||
$arg =~ /^0x[0-9A-Fa-f]{8}$/ ) {
Expand Down

0 comments on commit 45098e7

Please sign in to comment.