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 # Check if prefix is a number
32 isinteger prefix ||
return ${EXIT_FALSE}
34 [ ${prefix} -le 0 ] && return ${EXIT_FALSE}
35 [ ${prefix} -gt 32 ] && return ${EXIT_FALSE}
40 ipv4_netmask_is_valid
() {
43 # XXX this check could be much better by checking
44 # if the netmask only contains leading ones
46 ipv4_is_valid
"${netmask}"
49 ipv4_detect_duplicate
() {
55 assert device_exists
${device}
57 # Don't check on PPP devices.
58 device_is_ppp
${device} && return ${EXIT_ERROR}
60 if ! arping
-q -c 2 -w 3 -D -I ${device} ${address}; then
61 log DEBUG
"Detected duplicate address '${address}' on device '${device}'."
68 ipv4_update_neighbours
() {
72 # Don't do anything on PPP devices.
73 device_is_ppp
${device} && return ${EXIT_OK}
75 arping
-q -A -c 1 -I ${device} ${address}
76 ( sleep 2; arping
-q -U -c 1 -I ${device} ${address} ) >/dev
/null
2>&1 </dev
/null
&
79 ipv4_calculate_prefix
() {
83 local broadcast
="${2}"
85 inetcalc
-4 -p "${address}" "${broadcast}"
90 # Flushes all routes, addresses from the device
91 # and clears the ARP cache.
97 ip
-4 addr flush dev
${device} >/dev
/null
2>&1
98 ip
-4 route flush dev
${device} >/dev
/null
2>&1
99 ip
-4 neigh flush dev
${device} >/dev
/null
2>&1
104 ipv4_prefix2netmask
() {
109 echo "255.255.255.255"
112 echo "255.255.255.254"
115 echo "255.255.255.252"
118 echo "255.255.255.248"
121 echo "255.255.255.240"
124 echo "255.255.255.224"
127 echo "255.255.255.192"
130 echo "255.255.255.128"
215 ipv4_netmask2prefix
() {
222 for field
in ${netmask//\./ }; do
223 mask
=$
(( $
(( ${mask} << 8 )) | ${field} ))
227 local x="$(( 128 << 24 ))" # 0x80000000
229 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
230 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
231 cidr=$(( ${cidr} + 1 ))
234 assert [ $(( ${mask} & 2147483647 )) -eq 0 ]
243 ipv4_get_broadcast() {
244 inetcalc -4 -b $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
252 for field in ${addr//./ }; do
253 int=$(( $(( ${int} << 8 )) | ${field} ))
262 local addr=$(( ${int} & 255 ))
266 int=$(( ${int} >> 8 ))
267 addr
="$(( ${int} & 255 )).${addr}"
280 [[ "${addr1}" = "${addr2}" ]] \
281 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
290 inetcalc
-4 -g "${addr1}" "${addr2}" \
291 && return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
295 ipv4_addr_eq $@ || ipv4_addr_gt $@
299 ! ipv4_addr_eq $@
&& ! ipv4_addr_gt $@
303 ipv4_addr_eq $@ ||
! ipv4_addr_gt $@
312 _ipv4_range
"$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
319 if [ ${first} -gt ${last} ]; then
320 local range
="$(ipv4_decode ${first})-$(ipv4_decode ${last})"
322 error
"Invalid IPv4 address range: ${range}"
326 last
=$
(( ${last} + 1 ))
330 while [ ${last} -gt ${first} ]; do
336 while [ $
(( ${first} % ${y} )) -eq 0 ] && [ ${last} -gt $(( ${first} + ${y} )) ]; do
343 print
"$(ipv4_decode ${first})${prefix}"
344 first
=$
(( ${first} + ${z} ))
348 ipv4_range_explicit
() {
359 first
=$
(ipv4_get_network
${range})
360 first
="$(ip_split_prefix "${range}")"
361 last
=$
(ipv4_get_broadcast
${range})
362 last
="$(ip_split_prefix "${range}")"
366 _ipv4_range_explicit
"$(ipv4_encode ${first})" "$(ipv4_encode ${last})"
369 _ipv4_range_explicit
() {
373 if [ ${first} -gt ${last} ]; then
374 local range
="$(ipv4_decode ${first})-$(ipv4_decode ${last})"
376 error
"Invalid IPv4 address range: ${range}"
380 while [ ${first} -le ${last} ]; do
382 first
=$
(( ${first} + 1 ))
393 local subnet_first
=$
(ipv4_get_network_encoded
${subnet})
394 local subnet_last
=$
(ipv4_get_broadcast_encoded
${subnet})
396 addr
=$
(ipv4_encode
${addr})
398 if [[ "${addr}" -ge "${subnet_first}" ]] && [[ "${addr}" -le "${subnet_last}" ]]; then
408 isinteger ttl ||
return ${EXIT_FALSE}
410 # Must be between 10 and 255.
411 [ "${ttl}" -lt 10 ] && return ${EXIT_FALSE}
412 [ "${ttl}" -gt 255 ] && return ${EXIT_FALSE}