Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Coolkids committed Dec 15, 2023
2 parents 83d75f3 + f4a0b77 commit 9e37fc9
Show file tree
Hide file tree
Showing 101 changed files with 3,340 additions and 394 deletions.
4 changes: 2 additions & 2 deletions include/kernel-6.1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
LINUX_VERSION-6.1 = .65
LINUX_KERNEL_HASH-6.1.65 = 407229936802a44b1e484c2e9ac3bbe53a65d825cc468ccdbd76281b491ab20a
LINUX_VERSION-6.1 = .67
LINUX_KERNEL_HASH-6.1.67 = 7537db7289ca4854a126bc1237c47c5b21784bcbf27b4e571d389e3528c59285
232 changes: 136 additions & 96 deletions package/base-files/files/bin/ipcalc.sh
Original file line number Diff line number Diff line change
@@ -1,100 +1,140 @@
#!/usr/bin/awk -f

function bitcount(c) {
c=and(rshift(c, 1),0x55555555)+and(c,0x55555555)
c=and(rshift(c, 2),0x33333333)+and(c,0x33333333)
c=and(rshift(c, 4),0x0f0f0f0f)+and(c,0x0f0f0f0f)
c=and(rshift(c, 8),0x00ff00ff)+and(c,0x00ff00ff)
c=and(rshift(c,16),0x0000ffff)+and(c,0x0000ffff)
return c
#!/bin/sh

. /lib/functions/ipv4.sh

PROG="$(basename "$0")"

# wrapper to convert an integer to an address, unless we're using
# decimal output format.
# hook for library function
_ip2str() {
local var="$1" n="$2"
assert_uint32 "$n" || exit 1

if [ "$decimal" -ne 0 ]; then
export -- "$var=$n"
elif [ "$hexadecimal" -ne 0 ]; then
export -- "$var=$(printf "%x" "$n")"
else
ip2str "$@"
fi
}

function ip2int(ip) {
ret=0
n=split(ip,a,"\\.")
for (x=1;x<=n;x++)
ret=or(lshift(ret,8),a[x])
return ret
usage() {
echo "Usage: $PROG [ -d | -x ] address/prefix [ start limit ]" >&2
exit 1
}

function int2ip(ip,ret,x) {
ret=and(ip,255)
ip=rshift(ip,8)
for(;x<3;x++) {
ret=and(ip,255)"."ret
ip=rshift(ip,8)
}
return ret
}

function compl32(v) {
ret=xor(v, 0xffffffff)
return ret
}

BEGIN {
slpos=index(ARGV[1],"/")
if (slpos != 0) {
# rearrange arguments to not use compound notation
ARGV[4]=ARGV[3]
ARGV[3]=ARGV[2]
ARGV[2]=substr(ARGV[1],slpos+1)
ARGV[1]=substr(ARGV[1],0,slpos-1)
}
ipaddr=ip2int(ARGV[1])
dotpos=index(ARGV[2],".")
if (dotpos == 0)
netmask=compl32(2**(32-int(ARGV[2]))-1)
else
netmask=ip2int(ARGV[2])

network=and(ipaddr,netmask)
prefix=32-bitcount(compl32(netmask))

print "IP="int2ip(ipaddr)
print "NETMASK="int2ip(netmask)
print "NETWORK="int2ip(network)
if (prefix<=30) {
broadcast=or(network,compl32(netmask))
print "BROADCAST="int2ip(broadcast)
}
print "PREFIX="prefix

# range calculations:
# ipcalc <ip> <netmask> <range_start> <range_size>

if (ARGC <= 3)
exit(0)

if (prefix<=30)
limit=network+1
else
limit=network

start=or(network,and(ip2int(ARGV[3]),compl32(netmask)))
if (start<limit) start=limit
if (start==ipaddr) start=ipaddr+1

if (prefix<=30)
limit=or(network,compl32(netmask))-1
else
limit=or(network,compl32(netmask))

end=start+ARGV[4]-1
if (end>limit) end=limit
if (end==ipaddr) end=ipaddr-1

if (start>end) {
print "network ("int2ip(network)"/"prefix") too small" > "/dev/stderr"
exit(1)
}

if (ipaddr >= start && ipaddr <= end) {
print "warning: ipaddr inside range - this might not be supported in future releases of Openwrt" > "/dev/stderr"
# turn this into an error after Openwrt 24 has been released
# exit(1)
}

print "START="int2ip(start)
print "END="int2ip(end)
}
decimal=0
hexadecimal=0
if [ "$1" = "-d" ]; then
decimal=1
shift
elif [ "$1" = "-x" ]; then
hexadecimal=1
shift
fi

if [ $# -eq 0 ]; then
usage
fi

case "$1" in
*/*.*)
# data is n.n.n.n/m.m.m.m format, like on a Cisco router
str2ip ipaddr "${1%/*}" || exit 1
str2ip netmask "${1#*/}" || exit 1
netmask2prefix prefix "$netmask" || exit 1
shift
;;
*/*)
# more modern prefix notation of n.n.n.n/p
str2ip ipaddr "${1%/*}" || exit 1
prefix="${1#*/}"
assert_uint32 "$prefix" || exit 1
if [ "$prefix" -gt 32 ]; then
printf "Prefix out of range (%s)\n" "$prefix" >&2
exit 1
fi
prefix2netmask netmask "$prefix" || exit 1
shift
;;
*)
# address and netmask as two separate arguments
str2ip ipaddr "$1" || exit 1
str2ip netmask "$2" || exit 1
netmask2prefix prefix "$netmask" || exit 1
shift 2
;;
esac

# we either have no arguments left, or we have a range start and length
if [ $# -ne 0 ] && [ $# -ne 2 ]; then
usage
fi

# complement of the netmask, i.e. the hostmask
hostmask=$((netmask ^ 0xffffffff))
network=$((ipaddr & netmask))
broadcast=$((network | hostmask))
count=$((hostmask + 1))

_ip2str IP "$ipaddr"
_ip2str NETMASK "$netmask"
_ip2str NETWORK "$network"

echo "IP=$IP"
echo "NETMASK=$NETMASK"
# don't include this-network or broadcast addresses
if [ "$prefix" -le 30 ]; then
_ip2str BROADCAST "$broadcast"
echo "BROADCAST=$BROADCAST"
fi
echo "NETWORK=$NETWORK"
echo "PREFIX=$prefix"
echo "COUNT=$count"

# if there's no range, we're done
[ $# -eq 0 ] && exit 0

if [ "$prefix" -le 30 ]; then
lower=$((network + 1))
else
lower="$network"
fi

start="$1"
assert_uint32 "$start" || exit 1
start=$((network | (start & hostmask)))
[ "$start" -lt "$lower" ] && start="$lower"
[ "$start" -eq "$ipaddr" ] && start=$((start + 1))

if [ "$prefix" -le 30 ]; then
upper=$(((network | hostmask) - 1))
else
upper="$network"
fi

range="$2"
assert_uint32 "$range" || exit 1
end=$((start + range - 1))
[ "$end" -gt "$upper" ] && end="$upper"
[ "$end" -eq "$ipaddr" ] && end=$((end - 1))

if [ "$start" -gt "$end" ]; then
echo "network ($NETWORK/$prefix) too small" >&2
exit 1
fi

_ip2str START "$start"
_ip2str END "$end"

if [ "$start" -le "$ipaddr" ] && [ "$ipaddr" -le "$end" ]; then
echo "error: address $IP inside range $START..$END" >&2
exit 1
fi

echo "START=$START"
echo "END=$END"

exit 0
Loading

0 comments on commit 9e37fc9

Please sign in to comment.