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 # A list of supported versions of the IP protocol
23 IP_SUPPORTED_PROTOCOLS
=""
25 function ip_split_prefix
() {
33 function ip_get_prefix
() {
38 # Break if no prefix is provided
39 [[ ${address} =~ \
/ ]] ||
return ${EXIT_OK}
44 function ip_detect_protocol
() {
50 for protocol
in ${IP_SUPPORTED_PROTOCOLS}; do
51 if ${protocol}_is_valid
${address}; then
52 log DEBUG
"Address '${address}' was detected to be protocol '${protocol}'."
58 log DEBUG
"Protocol version of address '${address}' could not be detected."
63 function ip_protocol_is_supported
() {
68 listmatch
${proto} ${IP_SUPPORTED_PROTOCOLS}
71 function ip_address_add
() {
76 assert device_exists
${device}
78 local prefix
=$
(ip_get_prefix
${address})
79 address
=$
(ip_split_prefix
${address})
83 # Detect the protocol version
84 local protocol
=$
(ip_detect_protocol
${address}/${prefix})
85 assert ip_protocol_is_supported
${protocol}
89 if ipv4_detect_duplicate
${device} ${address}; then
90 error_log
"Duplicate address detected on zone '${device}' (${address})."
91 error_log
"Cannot continue."
97 if ! device_has_ip
${device} ${address}/${prefix}; then
98 assert ip addr add
${address}/${prefix} dev ${device}
100 log DEBUG
"IP address '${address}' (${protocol}) was successfully configured on device '${device}'."
102 case "${protocol}" in
104 # Announce our new address to the neighbours
105 ipv4_update_neighbours
${device} ${address}
109 log DEBUG
"IP address '${address}' (${protocol}) was already configured on device '${device}'."
115 function ip_address_del
() {
120 assert device_exists
${device}
122 local prefix
=$
(ip_get_prefix
${address})
123 address
=$
(ip_split_prefix
${address})
127 # Detect the protocol version
128 local protocol
=$
(ip_detect_protocol
${address}/${prefix})
129 assert ip_protocol_is_supported
${protocol}
131 if device_has_ip
${device} ${address}/${prefix}; then
132 assert ip addr del
${address}/${prefix} dev ${device}
134 log DEBUG
"IP address '${address}' (${protocol}) was successfully removed from device '${device}'."
136 log DEBUG
"IP address '${address}' (${protocol}) was not configured on device '${device}'."