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
() {
32 function ip_get_prefix
() {
36 # Break if no prefix is provided
37 [[ ${address} =~ \
/ ]] ||
return ${EXIT_OK}
42 function ip_detect_protocol
() {
48 for protocol
in ${IP_SUPPORTED_PROTOCOLS}; do
49 if ${protocol}_is_valid
${address}; then
58 function ip_protocol_is_supported
() {
63 listmatch
${proto} ${IP_SUPPORTED_PROTOCOLS}
66 function ip_is_valid
() {
70 local proto
=$
(ip_detect_protocol
${address})
71 isset proto
&& return ${EXIT_TRUE} ||
return ${EXIT_FALSE}
74 function ip_is_network
() {
78 # Get the address part.
79 local address
=$
(ip_split_prefix
${network})
80 isset address ||
return ${EXIT_FALSE}
83 local prefix
=$
(ip_get_prefix
${network})
84 isset prefix ||
return ${EXIT_FALSE}
86 # Detect the protocol.
87 local proto
=$
(ip_detect_protocol
${address})
90 # Check if the prefix is correct.
91 ip_prefix_is_valid
${proto} ${prefix} || return ${EXIT_FALSE}
96 function ip_prefix_is_valid
() {
104 ipv4_prefix_is_valid
${prefix}
108 ipv6_prefix_is_valid
${prefix}
113 assert ip_protocol_is_supported
${proto}
116 function ip_address_add
() {
121 assert device_exists
${device}
123 local prefix
=$
(ip_get_prefix
${address})
124 address
=$
(ip_split_prefix
${address})
128 # Detect the protocol version
129 local protocol
=$
(ip_detect_protocol
${address}/${prefix})
130 assert ip_protocol_is_supported
${protocol}
132 case "${protocol}" in
134 if ipv4_detect_duplicate
${device} ${address}; then
135 error_log
"Duplicate address detected on zone '${device}' (${address})."
136 error_log
"Cannot continue."
142 if ! device_has_ip
${device} ${address}/${prefix}; then
143 assert ip addr add
${address}/${prefix} dev ${device}
145 log DEBUG
"IP address '${address}' (${protocol}) was successfully configured on device '${device}'."
147 case "${protocol}" in
149 # Announce our new address to the neighbours
150 ipv4_update_neighbours
${device} ${address}
154 log DEBUG
"IP address '${address}' (${protocol}) was already configured on device '${device}'."
160 function ip_address_del
() {
165 assert device_exists
${device}
167 local prefix
=$
(ip_get_prefix
${address})
168 address
=$
(ip_split_prefix
${address})
172 # Detect the protocol version
173 local protocol
=$
(ip_detect_protocol
${address}/${prefix})
174 assert ip_protocol_is_supported
${protocol}
176 if device_has_ip
${device} ${address}/${prefix}; then
177 assert ip addr del
${address}/${prefix} dev ${device}
179 log DEBUG
"IP address '${address}' (${protocol}) was successfully removed from device '${device}'."
181 log DEBUG
"IP address '${address}' (${protocol}) was not configured on device '${device}'."