]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.ip
ip: Allow detection of protocol for networks, too
[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 proto=$(ip_detect_protocol ${address})
73 isset proto && return ${EXIT_TRUE} || return ${EXIT_FALSE}
74 }
75
76 ip_is_network() {
77 local network=${1}
78 assert isset network
79
80 # Get the address part.
81 local address=$(ip_split_prefix ${network})
82 isset address || return ${EXIT_FALSE}
83
84 # Get the prefix.
85 local prefix=$(ip_get_prefix ${network})
86 isset prefix || return ${EXIT_FALSE}
87
88 # Detect the protocol.
89 local proto=$(ip_detect_protocol ${address})
90 assert isset proto
91
92 # Check if the prefix is correct.
93 ip_prefix_is_valid ${proto} ${prefix} || return ${EXIT_FALSE}
94
95 return ${EXIT_TRUE}
96 }
97
98 ip_prefix_is_valid() {
99 local proto=${1}
100 assert isset proto
101
102 local prefix=${2}
103
104 case "${proto}" in
105 ipv4)
106 ipv4_prefix_is_valid ${prefix}
107 return $?
108 ;;
109 ipv6)
110 ipv6_prefix_is_valid ${prefix}
111 return $?
112 ;;
113 esac
114
115 assert ip_protocol_is_supported ${proto}
116 }
117
118 ip_get_network() {
119 inetcalc -n $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
120 }
121
122 ip_address_add() {
123 local device=${1}
124 local address=${2}
125
126 assert isset address
127 assert device_exists ${device}
128
129 local prefix=$(ip_get_prefix ${address})
130 address=$(ip_split_prefix ${address})
131
132 assert isset prefix
133 assert isset address
134
135 echo "ADDRESS = $address"
136
137 # Detect the protocol version
138 local protocol=$(ip_detect_protocol "${address}")
139 assert ip_protocol_is_supported "${protocol}"
140
141 case "${protocol}" in
142 ipv6)
143 assert ipv6_prefix_is_valid "${prefix}"
144 ;;
145 ipv4)
146 assert ipv4_prefix_is_valid "${prefix}"
147 ;;
148 esac
149
150 case "${protocol}" in
151 ipv4)
152 if ipv4_detect_duplicate ${device} ${address}; then
153 error_log "Duplicate address detected on zone '${device}' (${address})."
154 error_log "Cannot continue."
155 return ${EXIT_ERROR}
156 fi
157 ;;
158 esac
159
160 if ! device_has_ip ${device} ${address}/${prefix}; then
161 assert ip addr add ${address}/${prefix} dev ${device}
162
163 log DEBUG "IP address '${address}' (${protocol}) was successfully configured on device '${device}'."
164
165 case "${protocol}" in
166 ipv4)
167 # Announce our new address to the neighbours
168 ipv4_update_neighbours ${device} ${address}
169 ;;
170 esac
171 else
172 log DEBUG "IP address '${address}' (${protocol}) was already configured on device '${device}'."
173 fi
174
175 return ${EXIT_OK}
176 }
177
178 ip_address_del() {
179 local device=${1}
180 local address=${2}
181
182 assert isset address
183 assert device_exists ${device}
184
185 local prefix=$(ip_get_prefix ${address})
186 address=$(ip_split_prefix ${address})
187
188 assert isset prefix
189
190 # Detect the protocol version
191 local protocol=$(ip_detect_protocol "${address}")
192 assert ip_protocol_is_supported "${protocol}"
193
194 if device_has_ip ${device} ${address}/${prefix}; then
195 assert ip addr del ${address}/${prefix} dev ${device}
196
197 log DEBUG "IP address '${address}' (${protocol}) was successfully removed from device '${device}'."
198 else
199 log DEBUG "IP address '${address}' (${protocol}) was not configured on device '${device}'."
200 fi
201
202 return ${EXIT_OK}
203 }