]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.ports
ports: Cannot delete a port that does not exist
[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 port_dir() {
23 local port="${1}"
24 echo "${NETWORK_CONFIG_DIR}/ports/${port}"
25 }
26
27 port_list() {
28 local port
29 for port in $(port_dir)/*; do
30 port="$(basename "${port}")"
31 if port_exists "${port}"; then
32 print "${port}"
33 fi
34 done
35 }
36
37 port_list_in_use() {
38 local ports_in_use
39
40 # Collect all ports that are attached to a zone
41 local zone
42 for zone in $(zones_get_all); do
43 list_append ports_in_use $(zone_get_ports "${zone}")
44 done
45
46 # Collect all ports that are enslaved by an other port
47 local port
48 for port in $(port_list); do
49 list_append ports_in_use $(port_get_slaves "${port}")
50 done
51
52 list_sort ${ports_in_use}
53 }
54
55 port_list_free() {
56 local ports_in_use="$(port_list_in_use)"
57
58 local port
59 for port in $(port_list); do
60 if ! list_match "${port}" ${ports_in_use}; then
61 print "${port}"
62 fi
63 done
64
65 return ${EXIT_OK}
66 }
67
68 port_get_hook() {
69 local port=${1}
70 assert isset port
71
72 config_get_hook $(port_file ${port})
73 }
74
75 port_config_dir() {
76 local port=${1}
77
78 print "${RUN_DIR}/ports/${port}"
79 return ${EXIT_OK}
80 }
81
82 port_settings_read() {
83 local port="${1}"
84 assert isset port
85
86 # Save the HOOK variable.
87 local hook="${HOOK}"
88
89 settings_read "$(port_file "${port}")" ${HOOK_SETTINGS}
90
91 # Restore hook.
92 HOOK="${hook}"
93 }
94
95 port_settings_write() {
96 local port="${1}"
97 assert isset port
98 shift
99
100 local args
101 if function_exists "hook_check_settings"; then
102 list_append args "--check=\"hook_check_settings\""
103 fi
104 list_append args ${HOOK_SETTINGS}
105
106 settings_write "$(port_file "${port}")" ${args}
107 }
108
109 ports_get_all() {
110 port_list
111 }
112
113 port_file() {
114 local port="${1}"
115 assert isset port
116
117 echo "$(port_dir ${port})/settings"
118 }
119
120 port_exists() {
121 local port=${1}
122
123 [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ]
124 }
125
126 port_get_hook() {
127 local port=${1}
128
129 assert isset port
130
131 config_get_hook $(port_file ${port})
132 }
133
134 port_is_attached() {
135 local port=${1}
136 shift
137
138 assert isset port
139
140 local zone
141 for zone in $(zones_get_all); do
142
143 assert isset zone
144 assert zone_exists ${zone}
145
146 if list_match ${port} $(zone_get_ports ${zone}); then
147 echo "${zone}"
148 return ${EXIT_OK}
149 fi
150 done
151
152 return ${EXIT_ERROR}
153 }
154
155 port_is_up() {
156 device_is_up $@
157 }
158
159 port_new() {
160 local hook="${1}"
161 shift
162
163 if ! hook_exists port "${hook}"; then
164 error "Port hook '${hook}' does not exist."
165 return ${EXIT_ERROR}
166 fi
167
168 hook_exec port "${hook}" new $@
169 }
170
171 port_destroy() {
172 local port=${1}
173 assert isset port
174
175 # Cannot delete a port that does not exist
176 if ! port_exists ${port}; then
177 error "No such port: ${port}"
178 return ${EXIT_ERROR}
179 fi
180
181 # Check if the port is attached to any zone and don't delete it.
182 local ok=${EXIT_OK}
183
184 local attached_zone=$(port_is_attached ${port})
185 if [ -n "${attached_zone}" ]; then
186 error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'."
187 ok=${EXIT_ERROR}
188 fi
189
190 # Check if the port is linked to any other port and don't allow the user
191 # to delete it.
192 local other_port
193 for other_port in $(ports_get); do
194 [ "${other_port}" = "${port}" ] && continue
195
196 if list_match ${port} $(port_get_parents ${other_port}); then
197 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
198 ok=${EXIT_ERROR}
199 fi
200
201 if list_match ${port} $(port_get_children ${other_port}); then
202 error_log "Cannot destroy port '${port}' which is child of port '${other_port}'."
203 ok=${EXIT_ERROR}
204 fi
205 done
206
207 # If ok says we are not okay --> exit
208 if [ ${ok} -ne ${EXIT_OK} ]; then
209 return ${EXIT_ERROR}
210 fi
211
212 port_remove "${port}"
213
214 rm -rf $(port_dir ${port})
215 }
216
217 port_create() {
218 port_cmd "create" $@
219 }
220
221 port_remove() {
222 local port="${1}"
223 assert isset port
224
225 if ! port_exists "${port}"; then
226 log ERROR "Port ${port} does not exist"
227 return ${EXIT_ERROR}
228 fi
229
230 # If the device is still up, we need to bring it down first.
231 if device_is_up "${port}"; then
232 port_down "${port}"
233 fi
234
235 port_cmd "remove" "${port}"
236 }
237
238 # Restarts the port by removing it and then re-creating it
239 port_restart() {
240 local port="${1}"
241 assert isset port
242
243 port_remove "${port}"
244
245 port_create "${port}"
246 }
247
248 port_edit() {
249 port_cmd edit $@
250 }
251
252 port_up() {
253 port_cmd up $@
254 }
255
256 port_down() {
257 port_cmd down $@
258 }
259
260 port_status() {
261 port_cmd status $@
262 }
263
264 port_info() {
265 port_cmd info $@
266 }
267
268 port_cmd() {
269 local cmd=${1}
270 local port=${2}
271 shift 2
272
273 assert isset cmd
274 assert isset port
275
276 local hook=$(port_get_hook ${port})
277
278 # Abort if we could not find a hook
279 if ! isset hook; then
280 log CRITICAL "Port ${port} does not have a hook associated with it"
281 return ${EXIT_ERROR}
282 fi
283
284 hook_exec port ${hook} ${cmd} ${port} $@
285 }
286
287 ports_get() {
288 local port
289 for port in $(port_dir)/*; do
290 port=$(basename ${port})
291 if port_exists ${port}; then
292 echo "${port}"
293 fi
294 done
295 }
296
297 port_find_free() {
298 local pattern=${1}
299
300 assert isset pattern
301
302 local port
303 local i=0
304
305 while [ ${i} -lt 99 ]; do
306 port=${pattern//N/${i}}
307 if ! port_exists ${port} && ! device_exists ${port}; then
308 echo "${port}"
309 return ${EXIT_OK}
310 fi
311 i=$(( ${i} + 1 ))
312 done
313
314 return ${EXIT_ERROR}
315 }
316
317 port_get_info() {
318 local port=${1}
319 local key=${2}
320
321 assert isset port
322 assert port_exists ${port}
323 assert isset key
324
325 (
326 eval $(port_info ${port})
327 echo "${!key}"
328 )
329 }
330
331 port_get_parents() {
332 local port=${1}
333
334 port_get_info ${port} PORT_PARENTS
335 }
336
337 port_get_children() {
338 local port=${1}
339
340 port_get_info ${port} PORT_CHILDREN
341 }
342
343 port_zone() {
344 # Get name of the zones, this port is configured in.
345 local port=${1}
346 shift
347
348 assert isset port
349
350 local zone
351 for zone in $(zones_get_all); do
352 if zone_has_port ${zone} ${port}; then
353 echo "${zone}"
354 return ${EXIT_OK}
355 fi
356 done
357
358 return ${EXIT_OK}
359 }
360
361 port_hotplug_event() {
362 local port="${1}"
363 assert isset port
364
365 hotplug_assert_in_hotplug_event
366
367 port_cmd "hotplug" "${port}"
368 }
369
370 port_get_slaves() {
371 local port="${1}"
372
373 port_settings_read "${port}" \
374 --ignore-superfluous-settings SLAVES
375 print "${SLAVES}"
376 }
377
378 port_device_is_slave() {
379 assert [ $# -eq 2 ]
380
381 local port="${1}"
382 local device="${2}"
383
384 # Get slaves of port
385 local slaves="$(port_get_slaves "${port}")"
386
387 # Returns true if device is in slaves
388 list_match "${device}" ${slaves}
389 }
390
391 port_get_phy() {
392 local port="${1}"
393
394 port_settings_read "${port}" \
395 --ignore-superfluous-settings PHY
396 print "${PHY}"
397 }
398
399 port_uses_phy() {
400 assert [ $# -eq 2 ]
401
402 local port="${1}"
403 local phy="${2}"
404
405 # Nothing to do if an empty argument is given
406 if ! isset phy; then
407 return ${EXIT_FALSE}
408 fi
409
410 phy="$(phy_get_address "${phy}")"
411
412 local port_phy="$(port_get_phy "${port}")"
413 [ "${port_phy}" = "${phy}" ]
414 }
415
416 ports_lowest_address() {
417 local address
418 local addresses
419
420 local port
421 for port in $(port_list); do
422 # Skip all ports that do not exist
423 # any more or are not plugged in
424 device_exists "${port}" || continue
425
426 # Skip all ports that are not proper ethernet devices
427 device_is_wireless "${port}" && continue
428 device_is_ethernet "${port}" || continue
429
430 list_append addresses "$(device_get_address "${port}")"
431 done
432
433 # Sort the list
434 addresses="$(list_sort ${addresses})"
435
436 # Get the first element which is the lowest MAC address
437 list_head ${addresses}
438 }
439
440 port_identify() {
441 device_identify $@
442 }
443
444 port_get_color() {
445 # This function return the color of a port
446 assert [ $# -eq 1 ]
447
448 local name=${1}
449 color_read "port" ${name}
450 }
451
452 port_get_description_title() {
453 assert [ $# -eq 1 ]
454
455 local name=${1}
456 description_title_read $(description_format_filename "port" "${name}")
457 }