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_MANPAGE
="network-config-ipv4-static"
26 HOOK_CONFIG_SETTINGS
="HOOK ADDRESS PREFIX GATEWAY"
28 hook_check_config_settings
() {
30 assert isinteger PREFIX
32 if [ ${PREFIX} -gt 30 ]; then
33 error
"PREFIX is greater than 30."
38 hook_parse_cmdline
() {
42 local key
="$(cli_get_key "${arg}")"
43 local val
="$(cli_get_val "${arg}")"
47 if ! ipv4_is_valid
"${val}"; then
48 error
"Invalid IPv4 address: ${val}"
49 exit ${EXIT_CONF_ERROR}
56 if ! ipv4_prefix_is_valid
"${val}"; then
57 error
"Invalid IPv4 prefix: ${val}"
58 exit ${EXIT_CONF_ERROR}
65 if ! ipv4_is_valid
"${val}"; then
66 error
"Invalid IPv4 address for gateway: ${val}"
67 exit ${EXIT_CONF_ERROR}
73 # Compatibility switches
75 if ! ipv4_netmask_is_valid
"${val}"; then
76 error
"Invalid netmask: ${val}"
77 exit ${EXIT_CONF_ERROR}
80 # The netmask will be converted into a prefix
81 PREFIX
="$(ipv4_netmask2prefix ${val})"
86 error
"Unhandled argument: ${arg}"
87 exit ${EXIT_CONF_ERROR}
92 if ! isset ADDRESS
; then
93 error
"You need to provide an IPv4 address"
94 exit ${EXIT_CONF_ERROR}
97 if ! isset PREFIX
; then
98 error
"You need to provide an IPv4 prefix"
99 exit ${EXIT_CONF_ERROR}
102 if zone_config_check_same_setting
"${zone}" "ipv4-static" "ADDRESS" "${ADDRESS}"; then
103 error
"An ipv4-static config with the same IPv4 address is already configured"
104 exit ${EXIT_CONF_ERROR}
107 if ! isset GATEWAY
&& zone_is_nonlocal
"${zone}"; then
108 warning
"You did not configure a gateway for a non-local zone"
116 assert zone_exists
"${zone}"
118 if ! hook_parse_cmdline $@
; then
119 # Return an error if the parsing of the cmd line fails
123 zone_config_settings_write
"${zone}" "${HOOK}"
133 if ! device_exists
${zone}; then
134 error
"Zone '${zone}' doesn't exist."
138 zone_config_settings_read
"${zone}" "${config}"
140 ip_address_add
${zone} ${ADDRESS}/${PREFIX}
143 db_set
"${zone}/ipv4/type" "${HOOK}"
144 db_set
"${zone}/ipv4/local-ip-address" "${ADDRESS}/${PREFIX}"
145 db_set
"${zone}/ipv4/remote-ip-address" "${GATEWAY}"
146 db_set
"${zone}/ipv4/active" 1
148 routing_update
${zone} ipv4
149 routing_default_update
159 if ! device_exists
${zone}; then
160 error
"Zone '${zone}' doesn't exist."
164 # Remove routing information from database.
165 db_delete
"${zone}/ipv4"
167 zone_config_settings_read
"${zone}" "${config}"
169 ip_address_del
${zone} ${ADDRESS}/${PREFIX}
171 # Update routing tables.
172 routing_default_update
186 if ! device_exists
${zone}; then
187 error
"Zone '${zone}' doesn't exist."
191 zone_config_settings_read
"${zone}" "${config}"
194 if zone_has_ip
${zone} ${ADDRESS}/${PREFIX}; then
195 status
=${MSG_HOOK_UP}
197 status
=${MSG_HOOK_DOWN}
199 cli_statusline
3 "${HOOK}" "${status}"
201 cli_print_fmt1
3 "IPv4 address" "${ADDRESS}/${PREFIX}"
202 if [ -n "${GATEWAY}" ]; then
203 cli_print_fmt1
3 "Gateway" "${GATEWAY}"