]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.ports
ports: Cannot delete a port that does not exist
[people/ms/network.git] / src / functions / functions.ports
CommitLineData
711ffac1 1#!/bin/bash
1578dae9
MT
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###############################################################################
711ffac1 21
1c6a4e30 22port_dir() {
af51d3a9
JS
23 local port="${1}"
24 echo "${NETWORK_CONFIG_DIR}/ports/${port}"
711ffac1
MT
25}
26
1c6a4e30 27port_list() {
bae37360
MT
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
1c6a4e30 37port_list_in_use() {
bae37360
MT
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
1c6a4e30 55port_list_free() {
bae37360
MT
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
1c6a4e30 68port_get_hook() {
49ec20d8
MT
69 local port=${1}
70 assert isset port
71
72 config_get_hook $(port_file ${port})
73}
74
1c6a4e30 75port_config_dir() {
49ec20d8
MT
76 local port=${1}
77
78 print "${RUN_DIR}/ports/${port}"
79 return ${EXIT_OK}
80}
81
1c6a4e30 82port_settings_read() {
e9df08ad 83 local port="${1}"
49ec20d8
MT
84 assert isset port
85
86 # Save the HOOK variable.
87 local hook="${HOOK}"
88
1e6f187e 89 settings_read "$(port_file "${port}")" ${HOOK_SETTINGS}
49ec20d8
MT
90
91 # Restore hook.
92 HOOK="${hook}"
93}
94
1c6a4e30 95port_settings_write() {
e9df08ad 96 local port="${1}"
49ec20d8 97 assert isset port
e9df08ad 98 shift
49ec20d8 99
1e6f187e
MT
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}
49ec20d8
MT
107}
108
1c6a4e30 109ports_get_all() {
bae37360 110 port_list
8895cf8f
MT
111}
112
1c6a4e30 113port_file() {
e9df08ad 114 local port="${1}"
711ffac1
MT
115 assert isset port
116
af51d3a9 117 echo "$(port_dir ${port})/settings"
711ffac1
MT
118}
119
1c6a4e30 120port_exists() {
711ffac1
MT
121 local port=${1}
122
af51d3a9 123 [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ]
711ffac1
MT
124}
125
1c6a4e30 126port_get_hook() {
711ffac1
MT
127 local port=${1}
128
129 assert isset port
130
131 config_get_hook $(port_file ${port})
132}
133
1c6a4e30 134port_is_attached() {
711ffac1
MT
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
8c9205b1 146 if list_match ${port} $(zone_get_ports ${zone}); then
711ffac1
MT
147 echo "${zone}"
148 return ${EXIT_OK}
149 fi
150 done
151
152 return ${EXIT_ERROR}
153}
154
1c6a4e30 155port_is_up() {
abba34c1
MT
156 device_is_up $@
157}
158
1c6a4e30 159port_new() {
ed75c16d 160 local hook="${1}"
711ffac1
MT
161 shift
162
ed75c16d 163 if ! hook_exists port "${hook}"; then
711ffac1
MT
164 error "Port hook '${hook}' does not exist."
165 return ${EXIT_ERROR}
166 fi
167
ed75c16d 168 hook_exec port "${hook}" new $@
711ffac1
MT
169}
170
1c6a4e30 171port_destroy() {
711ffac1 172 local port=${1}
711ffac1
MT
173 assert isset port
174
ab5edf53
MT
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
711ffac1 180
98f4dae6
MT
181 # Check if the port is attached to any zone and don't delete it.
182 local ok=${EXIT_OK}
711ffac1 183
98f4dae6 184 local attached_zone=$(port_is_attached ${port})
711ffac1 185 if [ -n "${attached_zone}" ]; then
98f4dae6
MT
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
8c9205b1 196 if list_match ${port} $(port_get_parents ${other_port}); then
98f4dae6
MT
197 error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'."
198 ok=${EXIT_ERROR}
199 fi
200
8c9205b1 201 if list_match ${port} $(port_get_children ${other_port}); then
98f4dae6
MT
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
711ffac1
MT
209 return ${EXIT_ERROR}
210 fi
211
510d8184 212 port_remove "${port}"
711ffac1 213
af51d3a9 214 rm -rf $(port_dir ${port})
711ffac1
MT
215}
216
1c6a4e30 217port_create() {
1ba6a2bb
MT
218 port_cmd "create" $@
219}
220
1c6a4e30 221port_remove() {
1ba6a2bb
MT
222 local port="${1}"
223 assert isset port
224
270aab39
MT
225 if ! port_exists "${port}"; then
226 log ERROR "Port ${port} does not exist"
227 return ${EXIT_ERROR}
228 fi
229
1ba6a2bb
MT
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}"
f90e550b
MT
236}
237
270aab39
MT
238# Restarts the port by removing it and then re-creating it
239port_restart() {
240 local port="${1}"
241 assert isset port
242
243 port_remove "${port}"
244
245 port_create "${port}"
246}
247
1c6a4e30 248port_edit() {
711ffac1
MT
249 port_cmd edit $@
250}
251
1c6a4e30 252port_up() {
711ffac1
MT
253 port_cmd up $@
254}
255
1c6a4e30 256port_down() {
711ffac1
MT
257 port_cmd down $@
258}
259
1c6a4e30 260port_status() {
711ffac1
MT
261 port_cmd status $@
262}
263
1c6a4e30 264port_info() {
98f4dae6
MT
265 port_cmd info $@
266}
267
1c6a4e30 268port_cmd() {
711ffac1
MT
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
0c949dd5
MT
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
711ffac1
MT
283
284 hook_exec port ${hook} ${cmd} ${port} $@
285}
f90e550b 286
1c6a4e30 287ports_get() {
f90e550b
MT
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}
2ae0fb8d 296
1c6a4e30 297port_find_free() {
d76f5107
MT
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}"
a1a8f0f4 309 return ${EXIT_OK}
d76f5107
MT
310 fi
311 i=$(( ${i} + 1 ))
312 done
a1a8f0f4
MT
313
314 return ${EXIT_ERROR}
d76f5107 315}
98f4dae6 316
1c6a4e30 317port_get_info() {
98f4dae6
MT
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
1c6a4e30 331port_get_parents() {
98f4dae6
MT
332 local port=${1}
333
334 port_get_info ${port} PORT_PARENTS
335}
336
1c6a4e30 337port_get_children() {
98f4dae6
MT
338 local port=${1}
339
340 port_get_info ${port} PORT_CHILDREN
341}
3a7fef62 342
1c6a4e30 343port_zone() {
3a7fef62
MT
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}
b8026986 360
1c6a4e30 361port_hotplug_event() {
b8026986
MT
362 local port="${1}"
363 assert isset port
364
365 hotplug_assert_in_hotplug_event
366
367 port_cmd "hotplug" "${port}"
368}
369
1c6a4e30 370port_get_slaves() {
b8026986
MT
371 local port="${1}"
372
373 port_settings_read "${port}" \
374 --ignore-superfluous-settings SLAVES
375 print "${SLAVES}"
376}
377
1c6a4e30 378port_device_is_slave() {
b8026986
MT
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
1c6a4e30 391port_get_phy() {
b8026986
MT
392 local port="${1}"
393
394 port_settings_read "${port}" \
395 --ignore-superfluous-settings PHY
396 print "${PHY}"
397}
398
1c6a4e30 399port_uses_phy() {
b8026986
MT
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}
7ad5252c 415
1c6a4e30 416ports_lowest_address() {
7ad5252c
MT
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}
f5ee091e
MT
439
440port_identify() {
441 device_identify $@
442}
410d2e85
JS
443
444port_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}
5de34b4c
JS
451
452port_get_description_title() {
453 assert [ $# -eq 1 ]
454
455 local name=${1}
456 description_title_read $(description_format_filename "port" "${name}")
457}