]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.ports
Fix hook settings writing and checking
[people/stevee/network.git] / src / functions / functions.ports
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 function port_dir() {
23 echo "${NETWORK_CONFIG_DIR}/ports"
24 }
25
26 function port_get_hook() {
27 local port=${1}
28 assert isset port
29
30 config_get_hook $(port_file ${port})
31 }
32
33 function port_config_dir() {
34 local port=${1}
35
36 print "${RUN_DIR}/ports/${port}"
37 return ${EXIT_OK}
38 }
39
40 function port_settings_read() {
41 local port="${1}"
42 assert isset port
43
44 # Save the HOOK variable.
45 local hook="${HOOK}"
46
47 settings_read "$(port_file "${port}")" ${HOOK_SETTINGS}
48
49 # Restore hook.
50 HOOK="${hook}"
51 }
52
53 function port_settings_write() {
54 local port="${1}"
55 assert isset port
56 shift
57
58 local args
59 if function_exists "hook_check_settings"; then
60 list_append args "--check=\"hook_check_settings\""
61 fi
62 list_append args ${HOOK_SETTINGS}
63
64 settings_write "$(port_file "${port}")" ${args}
65 }
66
67 function ports_get_all() {
68 local port
69
70 for port in $(port_dir)/*; do
71 [ -f "${port}" ] || continue
72
73 basename ${port}
74 done
75 }
76
77 function port_file() {
78 local port="${1}"
79 assert isset port
80
81 echo "$(port_dir)/${port}"
82 }
83
84 function port_exists() {
85 local port=${1}
86
87 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
88 }
89
90 function port_get_hook() {
91 local port=${1}
92
93 assert isset port
94
95 config_get_hook $(port_file ${port})
96 }
97
98 function port_is_attached() {
99 local port=${1}
100 shift
101
102 assert isset port
103
104 local zone
105 for zone in $(zones_get_all); do
106
107 assert isset zone
108 assert zone_exists ${zone}
109
110 if listmatch ${port} $(zone_get_ports ${zone}); then
111 echo "${zone}"
112 return ${EXIT_OK}
113 fi
114 done
115
116 return ${EXIT_ERROR}
117 }
118
119 function port_new() {
120 #local port=${1}
121 #shift
122 #
123 #if port_exists ${port}; then
124 # error "Port '${port}' does already exist."
125 # return ${EXIT_ERROR}
126 #fi
127
128 local hook=${1}
129 shift
130
131 if ! hook_exists port ${hook}; then
132 error "Port hook '${hook}' does not exist."
133 return ${EXIT_ERROR}
134 fi
135
136 #port_edit ${port} ${hook} $@
137 #
138 #if [ $? -ne ${EXIT_OK} ]; then
139 # port_destroy ${port}
140 #fi
141
142 hook_exec port ${hook} new $@
143 }
144
145 function port_destroy() {
146 local port=${1}
147
148 assert isset port
149
150 port_exists ${port} || return ${EXIT_OK}
151
152 # Check if the port is attached to any zone and don't delete it.
153 local ok=${EXIT_OK}
154
155 local attached_zone=$(port_is_attached ${port})
156 if [ -n "${attached_zone}" ]; then
157 error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
158 ok=${EXIT_ERROR}
159 fi
160
161 # Check if the port is linked to any other port and don't allow the user
162 # to delete it.
163 local other_port
164 for other_port in $(ports_get); do
165 [ "${other_port}" = "${port}" ] && continue
166
167 if listmatch ${port} $(port_get_parents ${other_port}); then
168 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
169 ok=${EXIT_ERROR}
170 fi
171
172 if listmatch ${port} $(port_get_children ${other_port}); then
173 error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
174 ok=${EXIT_ERROR}
175 fi
176 done
177
178 # If ok says we are not okay --> exit
179 if [ ${ok} -ne ${EXIT_OK} ]; then
180 return ${EXIT_ERROR}
181 fi
182
183 port_down ${port}
184
185 rm -f $(port_file ${port})
186 }
187
188 function port_create() {
189 port_cmd "create" $@
190 }
191
192 function port_remove() {
193 local port="${1}"
194 assert isset port
195
196 # If the device is still up, we need to bring it down first.
197 if device_is_up "${port}"; then
198 port_down "${port}"
199 fi
200
201 port_cmd "remove" "${port}"
202 }
203
204 function port_edit() {
205 port_cmd edit $@
206 }
207
208 # XXX? Compatibility function
209 function port_show() {
210 port_status $@
211 }
212
213 function port_up() {
214 port_cmd up $@
215 }
216
217 function port_down() {
218 port_cmd down $@
219 }
220
221 function port_status() {
222 port_cmd status $@
223 }
224
225 function port_info() {
226 port_cmd info $@
227 }
228
229 function port_cmd() {
230 local cmd=${1}
231 local port=${2}
232 shift 2
233
234 assert isset cmd
235 assert isset port
236
237 local hook=$(port_get_hook ${port})
238
239 assert isset hook
240
241 hook_exec port ${hook} ${cmd} ${port} $@
242 }
243
244 function ports_get() {
245 local port
246 for port in $(port_dir)/*; do
247 port=$(basename ${port})
248 if port_exists ${port}; then
249 echo "${port}"
250 fi
251 done
252 }
253
254 function port_find_free() {
255 local pattern=${1}
256
257 assert isset pattern
258
259 local port
260 local i=0
261
262 while [ ${i} -lt 99 ]; do
263 port=${pattern//N/${i}}
264 if ! port_exists ${port} && ! device_exists ${port}; then
265 echo "${port}"
266 return ${EXIT_OK}
267 fi
268 i=$(( ${i} + 1 ))
269 done
270
271 return ${EXIT_ERROR}
272 }
273
274 function port_get_info() {
275 local port=${1}
276 local key=${2}
277
278 assert isset port
279 assert port_exists ${port}
280 assert isset key
281
282 (
283 eval $(port_info ${port})
284 echo "${!key}"
285 )
286 }
287
288 function port_get_parents() {
289 local port=${1}
290
291 port_get_info ${port} PORT_PARENTS
292 }
293
294 function port_get_children() {
295 local port=${1}
296
297 port_get_info ${port} PORT_CHILDREN
298 }
299
300 function port_zone() {
301 # Get name of the zones, this port is configured in.
302 local port=${1}
303 shift
304
305 assert isset port
306
307 local zone
308 for zone in $(zones_get_all); do
309 if zone_has_port ${zone} ${port}; then
310 echo "${zone}"
311 return ${EXIT_OK}
312 fi
313 done
314
315 return ${EXIT_OK}
316 }
317
318 function port_hotplug_event() {
319 local port="${1}"
320 assert isset port
321
322 hotplug_assert_in_hotplug_event
323
324 port_cmd "hotplug" "${port}"
325 }
326
327 function port_get_slaves() {
328 local port="${1}"
329
330 port_settings_read "${port}" \
331 --ignore-superfluous-settings SLAVES
332 print "${SLAVES}"
333 }
334
335 function port_device_is_slave() {
336 assert [ $# -eq 2 ]
337
338 local port="${1}"
339 local device="${2}"
340
341 # Get slaves of port
342 local slaves="$(port_get_slaves "${port}")"
343
344 # Returns true if device is in slaves
345 list_match "${device}" ${slaves}
346 }
347
348 function port_get_phy() {
349 local port="${1}"
350
351 port_settings_read "${port}" \
352 --ignore-superfluous-settings PHY
353 print "${PHY}"
354 }
355
356 function port_uses_phy() {
357 assert [ $# -eq 2 ]
358
359 local port="${1}"
360 local phy="${2}"
361
362 # Nothing to do if an empty argument is given
363 if ! isset phy; then
364 return ${EXIT_FALSE}
365 fi
366
367 phy="$(phy_get_address "${phy}")"
368
369 local port_phy="$(port_get_phy "${port}")"
370 [ "${port_phy}" = "${phy}" ]
371 }