]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.ip
ip_is_valid: Refactor functions
[people/ms/network.git] / src / functions / functions.ip
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 # A list of supported versions of the IP protocol
23 IP_SUPPORTED_PROTOCOLS=""
24
25 ip_split_prefix() {
26 local address=${1}
27 assert isset address
28
29 echo "${address%%/*}"
30 }
31
32 ip_get_prefix() {
33 local address=${1}
34 assert isset address
35
36 # Break if no prefix is provided
37 [[ ${address} =~ \/ ]] || return ${EXIT_OK}
38
39 echo "${address##*/}"
40 }
41
42 ip_detect_protocol() {
43 local address="${1}"
44 assert isset address
45
46 # Remove prefix so that we can handle subnet, too
47 address=$(ip_split_prefix ${address})
48
49 local protocol
50 for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
51 if ${protocol}_is_valid "${address}"; then
52 echo "${protocol}"
53 return ${EXIT_OK}
54 fi
55 done
56
57 return ${EXIT_ERROR}
58 }
59
60 ip_protocol_is_supported() {
61 local proto=${1}
62
63 assert isset proto
64
65 listmatch ${proto} ${IP_SUPPORTED_PROTOCOLS}
66 }
67
68 ip_is_valid() {
69 local address=${1}
70 assert isset address
71
72 local protocol
73 for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
74 if ${protocol}_is_valid "${address}"; then
75 return ${EXIT_TRUE}
76 fi
77 done
78
79 return ${EXIT_FALSE}
80 }
81
82 ip_is_network() {
83 local network=${1}
84 assert isset network
85
86 # Get the address part.
87 local address=$(ip_split_prefix ${network})
88 isset address || return ${EXIT_FALSE}
89
90 # Get the prefix.
91 local prefix=$(ip_get_prefix ${network})
92 isset prefix || return ${EXIT_FALSE}
93
94 # Detect the protocol.
95 local proto=$(ip_detect_protocol ${address})
96 assert isset proto
97
98 # Check if the prefix is correct.
99 ip_prefix_is_valid ${proto} ${prefix} || return ${EXIT_FALSE}
100
101 return ${EXIT_TRUE}
102 }
103
104 ip_prefix_is_valid() {
105 local proto=${1}
106 assert isset proto
107
108 local prefix=${2}
109
110 case "${proto}" in
111 ipv4)
112 ipv4_prefix_is_valid ${prefix}
113 return $?
114 ;;
115 ipv6)
116 ipv6_prefix_is_valid ${prefix}
117 return $?
118 ;;
119 esac
120
121 assert ip_protocol_is_supported ${proto}
122 }
123
124 ip_get_network() {
125 inetcalc -n $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
126 }
127
128 ip_address_add() {
129 local device=${1}
130 local address=${2}
131
132 assert isset address
133 assert device_exists ${device}
134
135 local prefix=$(ip_get_prefix ${address})
136 address=$(ip_split_prefix ${address})
137
138 assert isset prefix
139 assert isset address
140
141 echo "ADDRESS = $address"
142
143 # Detect the protocol version
144 local protocol=$(ip_detect_protocol "${address}")
145 assert ip_protocol_is_supported "${protocol}"
146
147 case "${protocol}" in
148 ipv6)
149 assert ipv6_prefix_is_valid "${prefix}"
150 ;;
151 ipv4)
152 assert ipv4_prefix_is_valid "${prefix}"
153 ;;
154 esac
155
156 case "${protocol}" in
157 ipv4)
158 if ipv4_detect_duplicate ${device} ${address}; then
159 error_log "Duplicate address detected on zone '${device}' (${address})."
160 error_log "Cannot continue."
161 return ${EXIT_ERROR}
162 fi
163 ;;
164 esac
165
166 if ! device_has_ip ${device} ${address}/${prefix}; then
167 assert ip addr add ${address}/${prefix} dev ${device}
168
169 log DEBUG "IP address '${address}' (${protocol}) was successfully configured on device '${device}'."
170
171 case "${protocol}" in
172 ipv4)
173 # Announce our new address to the neighbours
174 ipv4_update_neighbours ${device} ${address}
175 ;;
176 esac
177 else
178 log DEBUG "IP address '${address}' (${protocol}) was already configured on device '${device}'."
179 fi
180
181 return ${EXIT_OK}
182 }
183
184 ip_address_del() {
185 local device=${1}
186 local address=${2}
187
188 assert isset address
189 assert device_exists ${device}
190
191 local prefix=$(ip_get_prefix ${address})
192 address=$(ip_split_prefix ${address})
193
194 assert isset prefix
195
196 # Detect the protocol version
197 local protocol=$(ip_detect_protocol "${address}")
198 assert ip_protocol_is_supported "${protocol}"
199
200 if device_has_ip ${device} ${address}/${prefix}; then
201 assert ip addr del ${address}/${prefix} dev ${device}
202
203 log DEBUG "IP address '${address}' (${protocol}) was successfully removed from device '${device}'."
204 else
205 log DEBUG "IP address '${address}' (${protocol}) was not configured on device '${device}'."
206 fi
207
208 return ${EXIT_OK}
209 }