]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.ip
7e47d07630346ff1ca581e2aef61a4017f2e8808
[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 function ip_split_prefix() {
26 local address=${1}
27 assert isset address
28
29 echo "${address%%/*}"
30 }
31
32 function 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 function ip_detect_protocol() {
43 local address=${1}
44
45 assert isset address
46
47 local protocol
48 for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
49 if ${protocol}_is_valid ${address}; then
50 echo "${protocol}"
51 return ${EXIT_OK}
52 fi
53 done
54
55 return ${EXIT_ERROR}
56 }
57
58 function ip_protocol_is_supported() {
59 local proto=${1}
60
61 assert isset proto
62
63 listmatch ${proto} ${IP_SUPPORTED_PROTOCOLS}
64 }
65
66 function ip_is_valid() {
67 local address=${1}
68 assert isset address
69
70 local proto=$(ip_detect_protocol ${address})
71 isset proto && return ${EXIT_TRUE} || return ${EXIT_FALSE}
72 }
73
74 function ip_is_network() {
75 local network=${1}
76 assert isset network
77
78 # Get the address part.
79 local address=$(ip_split_prefix ${network})
80 isset address || return ${EXIT_FALSE}
81
82 # Get the prefix.
83 local prefix=$(ip_get_prefix ${network})
84 isset prefix || return ${EXIT_FALSE}
85
86 # Detect the protocol.
87 local proto=$(ip_detect_protocol ${address})
88 assert isset proto
89
90 # Check if the prefix is correct.
91 ip_prefix_is_valid ${proto} ${prefix} || return ${EXIT_FALSE}
92
93 return ${EXIT_TRUE}
94 }
95
96 function 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 function ip_address_add() {
117 local device=${1}
118 local address=${2}
119
120 assert isset address
121 assert device_exists ${device}
122
123 local prefix=$(ip_get_prefix ${address})
124 address=$(ip_split_prefix ${address})
125
126 assert isset prefix
127
128 # Detect the protocol version
129 local protocol=$(ip_detect_protocol ${address}/${prefix})
130 assert ip_protocol_is_supported ${protocol}
131
132 case "${protocol}" in
133 ipv4)
134 if ipv4_detect_duplicate ${device} ${address}; then
135 error_log "Duplicate address detected on zone '${device}' (${address})."
136 error_log "Cannot continue."
137 return ${EXIT_ERROR}
138 fi
139 ;;
140 esac
141
142 if ! device_has_ip ${device} ${address}/${prefix}; then
143 assert ip addr add ${address}/${prefix} dev ${device}
144
145 log DEBUG "IP address '${address}' (${protocol}) was successfully configured on device '${device}'."
146
147 case "${protocol}" in
148 ipv4)
149 # Announce our new address to the neighbours
150 ipv4_update_neighbours ${device} ${address}
151 ;;
152 esac
153 else
154 log DEBUG "IP address '${address}' (${protocol}) was already configured on device '${device}'."
155 fi
156
157 return ${EXIT_OK}
158 }
159
160 function ip_address_del() {
161 local device=${1}
162 local address=${2}
163
164 assert isset address
165 assert device_exists ${device}
166
167 local prefix=$(ip_get_prefix ${address})
168 address=$(ip_split_prefix ${address})
169
170 assert isset prefix
171
172 # Detect the protocol version
173 local protocol=$(ip_detect_protocol ${address}/${prefix})
174 assert ip_protocol_is_supported ${protocol}
175
176 if device_has_ip ${device} ${address}/${prefix}; then
177 assert ip addr del ${address}/${prefix} dev ${device}
178
179 log DEBUG "IP address '${address}' (${protocol}) was successfully removed from device '${device}'."
180 else
181 log DEBUG "IP address '${address}' (${protocol}) was not configured on device '${device}'."
182 fi
183
184 return ${EXIT_OK}
185 }