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 .
/usr
/lib
/network
/header-config
24 HOOK_SETTINGS
="HOOK ADDRESS PREFIX GATEWAY"
26 function hook_check
() {
28 assert isinteger PREFIX
30 if [ ${PREFIX} -gt 30 ]; then
31 error
"PREFIX is greater than 30."
36 function hook_create
() {
41 while [ $# -gt 0 ]; do
44 ADDRESS
=${1#--address=}
47 NETMASK
=${1#--netmask=}
53 GATEWAY
=${1#--gateway=}
59 if [ -z "${PREFIX}" -a -n "${NETMASK}" ]; then
60 PREFIX
=$
(ipv4_mask_to_cidr
${NETMASK})
63 # XXX maybe we can add some hashing to identify a configuration again
64 zone_config_settings_write
"${zone}" "${HOOK}.$(uuid)" ${HOOK_SETTINGS}
74 if ! device_exists ${zone}; then
75 error "Zone
'${zone}' doesn
't exist."
79 zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
81 ip_address_add ${zone} ${ADDRESS}/${PREFIX}
84 routing_db_set ${zone} ipv4 type "${HOOK}"
85 routing_db_set ${zone} ipv4 local-ip-address "${ADDRESS}/${PREFIX}"
86 routing_db_set ${zone} ipv4 remote-ip-address "${GATEWAY}"
87 routing_db_set ${zone} ipv4 active 1
89 routing_update ${zone} ipv4
90 routing_default_update
95 function hook_down() {
100 if ! device_exists ${zone}; then
101 error "Zone '${zone}' doesn't exist.
"
105 zone_config_settings_read "${zone}" "${config}" ${HOOK_SETTINGS}
107 ip_address_del
${zone} ${ADDRESS}/${PREFIX}
109 # Update routing tables.
110 routing_default_update
115 function hook_status
() {
120 if ! device_exists
${zone}; then
121 error
"Zone '${zone}' doesn't exist."
125 zone_config_settings_read
"${zone}" "${config}" ${HOOK_SETTINGS}
128 if zone_has_ip ${zone} ${ADDRESS}/${PREFIX}; then
129 status=${MSG_HOOK_UP}
131 status=${MSG_HOOK_DOWN}
133 cli_statusline 3 "${HOOK}" "${status}"
135 cli_print_fmt1 3 "IPv4 address
" "${ADDRESS}/${PREFIX}"
136 if [ -n "${GATEWAY}" ]; then
137 cli_print_fmt1 3 "Gateway
" "${GATEWAY}"
144 function ipv4_mask_to_cidr() {
148 for field in $(tr '.' ' ' <<<${1}); do
149 mask=$(( $(( ${mask} << 8 )) | ${field} ))
153 local x=$(( 128 << 24 )) # 0x80000000
155 while [ $(( ${x} & ${mask} )) -ne 0 ]; do
156 [ ${mask} -eq ${x} ] && mask=0 || mask=$(( ${mask} << 1 ))
157 cidr=$((${cidr} + 1))
160 if [ $(( ${mask} & 2147483647 )) -ne 0 ]; then # 2147483647 = 0x7fffffff
161 echo "Invalid net mask
: $1" >&2