2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 IP_SUPPORTED_PROTOCOLS
="${IP_SUPPORTED_PROTOCOLS} ipv4"
25 inetcalc
-4 -c $@
&& return ${EXIT_OK} ||
return ${EXIT_ERROR}
28 ipv4_prefix_is_valid
() {
31 isset prefix ||
return ${EXIT_FALSE}
33 [ ${prefix} -le 0 ] && return ${EXIT_FALSE}
34 [ ${prefix} -gt 32 ] && return ${EXIT_FALSE}
39 ipv4_netmask_is_valid
() {
42 # XXX this check could be much better by checking
43 # if the netmask only contains leading ones
45 ipv4_is_valid
"${netmask}"
48 ipv4_detect_duplicate
() {
54 assert device_exists
${device}
56 # Don't check on PPP devices.
57 device_is_ppp
${device} && return ${EXIT_ERROR}
59 if ! arping
-q -c 2 -w 3 -D -I ${device} ${address}; then
60 log DEBUG
"Detected duplicate address '${address}' on device '${device}'."
67 ipv4_update_neighbours
() {
71 # Don't do anything on PPP devices.
72 device_is_ppp
${device} && return ${EXIT_OK}
74 arping
-q -A -c 1 -I ${device} ${address}
75 ( sleep 2; arping
-q -U -c 1 -I ${device} ${address} ) >/dev
/null
2>&1 </dev
/null
&
78 ipv4_calculate_prefix
() {
82 local broadcast
="${2}"
84 inetcalc
-4 -p "${address}" "${broadcast}"
89 # Flushes all routes, addresses from the device
90 # and clears the ARP cache.
96 ip
-4 addr flush dev
${device} >/dev
/null
2>&1
97 ip
-4 route flush dev
${device} >/dev
/null
2>&1
98 ip
-4 neigh flush dev
${device} >/dev
/null
2>&1
103 ipv4_prefix2netmask
() {
108 echo "255.255.255.255"
111 echo "255.255.255.254"
114 echo "255.255.255.252"
117 echo "255.255.255.248"
120 echo "255.255.255.240"
123 echo "255.255.255.224"
126 echo "255.255.255.192"
129 echo "255.255.255.128"
214 ipv4_netmask2prefix
() {
221 for field
in ${netmask//\./ }; do
222 mask
=$
(( $
(( ${mask} << 8 )) | ${field} ))
226 local x="$(( 128 << 24 ))" # 0x80000000
228 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
229 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
230 cidr=$(( ${cidr} + 1 ))
233 assert [ $(( ${mask} & 2147483647 )) -eq 0 ]
242 ipv4_get_broadcast() {
243 inetcalc -4 -b $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
251 for field in ${addr//./ }; do
252 int=$(( $(( ${int} << 8 )) | ${field} ))
261 local addr=$(( ${int} & 255 ))
265 int=$(( ${int} >> 8 ))
266 addr
="$(( ${int} & 255 )).${addr}"
279 [[ "${addr1}" = "${addr2}" ]] \
280 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
291 for addr
in addr1 addr2
; do
292 printf -v ${addr} "%s" "$(ip_encode ${!addr})"
295 [[ ${addr1} -gt ${addr2} ]] \
296 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
305 _ipv4_range
"$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
312 if [ ${first} -gt ${last} ]; then
313 local range
="$(ipv4_decode ${first})-$(ipv4_decode ${last})"
315 error
"Invalid IPv4 address range: ${range}"
319 last
=$
(( ${last} + 1 ))
323 while [ ${last} -gt ${first} ]; do
329 while [ $
(( ${first} % ${y} )) -eq 0 ] && [ ${last} -gt $(( ${first} + ${y} )) ]; do
336 print
"$(ipv4_decode ${first})${prefix}"
337 first
=$
(( ${first} + ${z} ))
341 ipv4_range_explicit
() {
352 first
=$
(ipv4_get_network
${range})
353 first
="$(ip_split_prefix "${range}")"
354 last
=$
(ipv4_get_broadcast
${range})
355 last
="$(ip_split_prefix "${range}")"
359 _ipv4_range_explicit
"$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
362 _ipv4_range_explicit
() {
366 if [ ${first} -gt ${last} ]; then
367 local range
="$(ipv4_decode ${first})-$(ipv4_decode ${last})"
369 error
"Invalid IPv4 address range: ${range}"
373 while [ ${first} -le ${last} ]; do
375 first
=$
(( ${first} + 1 ))
386 local subnet_first
=$
(ipv4_get_network_encoded
${subnet})
387 local subnet_last
=$
(ipv4_get_broadcast_encoded
${subnet})
389 addr
=$
(ipv4_encode
${addr})
391 if [[ "${addr}" -ge "${subnet_first}" ]] && [[ "${addr}" -le "${subnet_last}" ]]; then
401 isinteger ttl ||
return ${EXIT_FALSE}
403 # Must be between 10 and 255.
404 [ "${ttl}" -lt 10 ] && return ${EXIT_FALSE}
405 [ "${ttl}" -gt 255 ] && return ${EXIT_FALSE}