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