]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.ip
device queues: Fix listing queues
[people/stevee/network.git] / src / functions / functions.ip
CommitLineData
2b5c311d
MT
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
e617226b
MT
22# A list of supported versions of the IP protocol
23IP_SUPPORTED_PROTOCOLS=""
24
1c6a4e30 25ip_split_prefix() {
2b5c311d 26 local address=${1}
2b5c311d
MT
27 assert isset address
28
29 echo "${address%%/*}"
30}
31
1c6a4e30 32ip_get_prefix() {
2b5c311d 33 local address=${1}
2b5c311d
MT
34 assert isset address
35
38f61548
MT
36 # Break if no prefix is provided
37 [[ ${address} =~ \/ ]] || return ${EXIT_OK}
38
2b5c311d
MT
39 echo "${address##*/}"
40}
41
1c6a4e30 42ip_detect_protocol() {
13a6e69f 43 local address="${1}"
2b5c311d
MT
44 assert isset address
45
0af2168d
JS
46 # Remove prefix so that we can handle subnet, too
47 address=$(ip_split_prefix ${address})
48
2b5c311d 49 local protocol
e617226b 50 for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
13a6e69f 51 if ${protocol}_is_valid "${address}"; then
2b5c311d
MT
52 echo "${protocol}"
53 return ${EXIT_OK}
54 fi
55 done
56
57 return ${EXIT_ERROR}
58}
e617226b 59
1c6a4e30 60ip_protocol_is_supported() {
e617226b
MT
61 local proto=${1}
62
63 assert isset proto
64
65 listmatch ${proto} ${IP_SUPPORTED_PROTOCOLS}
66}
38f61548 67
1c6a4e30 68ip_is_valid() {
afb7d704
MT
69 local address=${1}
70 assert isset address
71
b2cb6736
JS
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}
afb7d704
MT
80}
81
1c6a4e30 82ip_is_network() {
cb965348
MT
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
9d58a3bf
JS
94 # Detect the protocol (if this fails, the
95 # address part is invalid)
cb965348 96 local proto=$(ip_detect_protocol ${address})
9d58a3bf 97 isset proto || return ${EXIT_FALSE}
cb965348
MT
98
99 # Check if the prefix is correct.
100 ip_prefix_is_valid ${proto} ${prefix} || return ${EXIT_FALSE}
101
102 return ${EXIT_TRUE}
103}
104
1c6a4e30 105ip_prefix_is_valid() {
cb965348
MT
106 local proto=${1}
107 assert isset proto
108
109 local prefix=${2}
110
111 case "${proto}" in
112 ipv4)
113 ipv4_prefix_is_valid ${prefix}
114 return $?
115 ;;
116 ipv6)
117 ipv6_prefix_is_valid ${prefix}
118 return $?
119 ;;
120 esac
121
122 assert ip_protocol_is_supported ${proto}
123}
124
13a6e69f
MT
125ip_get_network() {
126 inetcalc -n $@ && return ${EXIT_OK} || return ${EXIT_ERROR}
127}
128
4a7c3c02
MT
129ip_network_is_subset_of() {
130 assert [ $# -eq 2 ]
131
132 inetcalc -s $@ && return ${EXIT_TRUE} || return ${EXIT_FALSE}
133}
134
1c6a4e30 135ip_address_add() {
38f61548
MT
136 local device=${1}
137 local address=${2}
138
139 assert isset address
140 assert device_exists ${device}
141
142 local prefix=$(ip_get_prefix ${address})
143 address=$(ip_split_prefix ${address})
144
145 assert isset prefix
13a6e69f
MT
146 assert isset address
147
148 echo "ADDRESS = $address"
38f61548
MT
149
150 # Detect the protocol version
13a6e69f
MT
151 local protocol=$(ip_detect_protocol "${address}")
152 assert ip_protocol_is_supported "${protocol}"
153
154 case "${protocol}" in
155 ipv6)
156 assert ipv6_prefix_is_valid "${prefix}"
157 ;;
158 ipv4)
159 assert ipv4_prefix_is_valid "${prefix}"
160 ;;
161 esac
38f61548
MT
162
163 case "${protocol}" in
164 ipv4)
165 if ipv4_detect_duplicate ${device} ${address}; then
166 error_log "Duplicate address detected on zone '${device}' (${address})."
167 error_log "Cannot continue."
168 return ${EXIT_ERROR}
169 fi
170 ;;
171 esac
172
173 if ! device_has_ip ${device} ${address}/${prefix}; then
174 assert ip addr add ${address}/${prefix} dev ${device}
175
176 log DEBUG "IP address '${address}' (${protocol}) was successfully configured on device '${device}'."
177
178 case "${protocol}" in
179 ipv4)
180 # Announce our new address to the neighbours
181 ipv4_update_neighbours ${device} ${address}
182 ;;
183 esac
184 else
185 log DEBUG "IP address '${address}' (${protocol}) was already configured on device '${device}'."
186 fi
187
188 return ${EXIT_OK}
189}
190
1c6a4e30 191ip_address_del() {
38f61548
MT
192 local device=${1}
193 local address=${2}
194
195 assert isset address
196 assert device_exists ${device}
197
198 local prefix=$(ip_get_prefix ${address})
199 address=$(ip_split_prefix ${address})
200
201 assert isset prefix
202
203 # Detect the protocol version
13a6e69f
MT
204 local protocol=$(ip_detect_protocol "${address}")
205 assert ip_protocol_is_supported "${protocol}"
38f61548
MT
206
207 if device_has_ip ${device} ${address}/${prefix}; then
208 assert ip addr del ${address}/${prefix} dev ${device}
209
210 log DEBUG "IP address '${address}' (${protocol}) was successfully removed from device '${device}'."
211 else
212 log DEBUG "IP address '${address}' (${protocol}) was not configured on device '${device}'."
213 fi
214
215 return ${EXIT_OK}
216}