]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.ports
Drop port_get_parents function
[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
8c9205b1 142 if list_match ${port} $(zone_get_ports ${zone}); then
711ffac1
MT
143 echo "${zone}"
144 return ${EXIT_OK}
145 fi
146 done
147
148 return ${EXIT_ERROR}
149}
150
1c6a4e30 151port_is_up() {
abba34c1
MT
152 device_is_up $@
153}
154
1c6a4e30 155port_new() {
ed75c16d 156 local hook="${1}"
711ffac1
MT
157 shift
158
ed75c16d 159 if ! hook_exists port "${hook}"; then
711ffac1
MT
160 error "Port hook '${hook}' does not exist."
161 return ${EXIT_ERROR}
162 fi
163
ed75c16d 164 hook_exec port "${hook}" new $@
711ffac1
MT
165}
166
1c6a4e30 167port_destroy() {
711ffac1 168 local port=${1}
711ffac1
MT
169 assert isset port
170
ab5edf53
MT
171 # Cannot delete a port that does not exist
172 if ! port_exists ${port}; then
173 error "No such port: ${port}"
174 return ${EXIT_ERROR}
175 fi
711ffac1 176
98f4dae6 177 local attached_zone=$(port_is_attached ${port})
711ffac1 178 if [ -n "${attached_zone}" ]; then
27fff860
MT
179 if ! zone_port_detach "${attached_zone}" "${port}"; then
180 error "Could not remove port ${port} from zone ${zone}"
181 return ${EXIT_ERROR}
182 fi
98f4dae6
MT
183 fi
184
185 # Check if the port is linked to any other port and don't allow the user
186 # to delete it.
187 local other_port
188 for other_port in $(ports_get); do
189 [ "${other_port}" = "${port}" ] && continue
190
8c9205b1 191 if list_match ${port} $(port_get_children ${other_port}); then
7e2b4356
MT
192 log ERROR "Cannot destroy port '${port}' which is child of port '${other_port}'."
193 return ${EXIT_ERROR}
98f4dae6
MT
194 fi
195 done
196
510d8184 197 port_remove "${port}"
711ffac1 198
af51d3a9 199 rm -rf $(port_dir ${port})
711ffac1
MT
200}
201
1c6a4e30 202port_create() {
1ba6a2bb
MT
203 port_cmd "create" $@
204}
205
1c6a4e30 206port_remove() {
1ba6a2bb
MT
207 local port="${1}"
208 assert isset port
209
270aab39
MT
210 if ! port_exists "${port}"; then
211 log ERROR "Port ${port} does not exist"
212 return ${EXIT_ERROR}
213 fi
214
1ba6a2bb
MT
215 # If the device is still up, we need to bring it down first.
216 if device_is_up "${port}"; then
217 port_down "${port}"
218 fi
219
220 port_cmd "remove" "${port}"
f90e550b
MT
221}
222
270aab39
MT
223# Restarts the port by removing it and then re-creating it
224port_restart() {
225 local port="${1}"
226 assert isset port
227
228 port_remove "${port}"
229
230 port_create "${port}"
231}
232
1c6a4e30 233port_edit() {
711ffac1
MT
234 port_cmd edit $@
235}
236
1c6a4e30 237port_up() {
711ffac1
MT
238 port_cmd up $@
239}
240
1c6a4e30 241port_down() {
711ffac1
MT
242 port_cmd down $@
243}
244
1c6a4e30 245port_status() {
711ffac1
MT
246 port_cmd status $@
247}
248
1c6a4e30 249port_info() {
98f4dae6
MT
250 port_cmd info $@
251}
252
1c6a4e30 253port_cmd() {
711ffac1
MT
254 local cmd=${1}
255 local port=${2}
256 shift 2
257
258 assert isset cmd
259 assert isset port
260
261 local hook=$(port_get_hook ${port})
262
0c949dd5
MT
263 # Abort if we could not find a hook
264 if ! isset hook; then
265 log CRITICAL "Port ${port} does not have a hook associated with it"
266 return ${EXIT_ERROR}
267 fi
711ffac1
MT
268
269 hook_exec port ${hook} ${cmd} ${port} $@
270}
f90e550b 271
1c6a4e30 272ports_get() {
f90e550b
MT
273 local port
274 for port in $(port_dir)/*; do
275 port=$(basename ${port})
276 if port_exists ${port}; then
277 echo "${port}"
278 fi
279 done
280}
2ae0fb8d 281
1c6a4e30 282port_find_free() {
d76f5107
MT
283 local pattern=${1}
284
285 assert isset pattern
286
287 local port
288 local i=0
289
290 while [ ${i} -lt 99 ]; do
291 port=${pattern//N/${i}}
292 if ! port_exists ${port} && ! device_exists ${port}; then
293 echo "${port}"
a1a8f0f4 294 return ${EXIT_OK}
d76f5107
MT
295 fi
296 i=$(( ${i} + 1 ))
297 done
a1a8f0f4
MT
298
299 return ${EXIT_ERROR}
d76f5107 300}
98f4dae6 301
1c6a4e30 302port_get_info() {
98f4dae6
MT
303 local port=${1}
304 local key=${2}
305
306 assert isset port
307 assert port_exists ${port}
308 assert isset key
309
310 (
311 eval $(port_info ${port})
312 echo "${!key}"
313 )
314}
315
1c6a4e30 316port_get_children() {
98f4dae6
MT
317 local port=${1}
318
7da8cf02
MT
319 assert port_exists "${port}"
320
321 port_cmd "children" "${port}"
98f4dae6 322}
3a7fef62 323
1c6a4e30 324port_zone() {
3a7fef62
MT
325 # Get name of the zones, this port is configured in.
326 local port=${1}
327 shift
328
329 assert isset port
330
331 local zone
332 for zone in $(zones_get_all); do
333 if zone_has_port ${zone} ${port}; then
334 echo "${zone}"
335 return ${EXIT_OK}
336 fi
337 done
338
339 return ${EXIT_OK}
340}
b8026986 341
1c6a4e30 342port_hotplug_event() {
b8026986
MT
343 local port="${1}"
344 assert isset port
345
346 hotplug_assert_in_hotplug_event
347
348 port_cmd "hotplug" "${port}"
349}
350
1c6a4e30 351port_get_slaves() {
b8026986
MT
352 local port="${1}"
353
354 port_settings_read "${port}" \
355 --ignore-superfluous-settings SLAVES
356 print "${SLAVES}"
357}
358
1c6a4e30 359port_device_is_slave() {
b8026986
MT
360 assert [ $# -eq 2 ]
361
362 local port="${1}"
363 local device="${2}"
364
365 # Get slaves of port
366 local slaves="$(port_get_slaves "${port}")"
367
368 # Returns true if device is in slaves
369 list_match "${device}" ${slaves}
370}
371
1c6a4e30 372port_get_phy() {
b8026986
MT
373 local port="${1}"
374
375 port_settings_read "${port}" \
376 --ignore-superfluous-settings PHY
377 print "${PHY}"
378}
379
1c6a4e30 380port_uses_phy() {
b8026986
MT
381 assert [ $# -eq 2 ]
382
383 local port="${1}"
384 local phy="${2}"
385
386 # Nothing to do if an empty argument is given
387 if ! isset phy; then
388 return ${EXIT_FALSE}
389 fi
390
391 phy="$(phy_get_address "${phy}")"
392
393 local port_phy="$(port_get_phy "${port}")"
394 [ "${port_phy}" = "${phy}" ]
395}
7ad5252c 396
1c6a4e30 397ports_lowest_address() {
7ad5252c
MT
398 local address
399 local addresses
400
401 local port
402 for port in $(port_list); do
403 # Skip all ports that do not exist
404 # any more or are not plugged in
405 device_exists "${port}" || continue
406
407 # Skip all ports that are not proper ethernet devices
408 device_is_wireless "${port}" && continue
409 device_is_ethernet "${port}" || continue
410
411 list_append addresses "$(device_get_address "${port}")"
412 done
413
414 # Sort the list
415 addresses="$(list_sort ${addresses})"
416
417 # Get the first element which is the lowest MAC address
418 list_head ${addresses}
419}
f5ee091e
MT
420
421port_identify() {
422 device_identify $@
423}
410d2e85
JS
424
425port_get_color() {
426 # This function return the color of a port
427 assert [ $# -eq 1 ]
428
429 local name=${1}
430 color_read "port" ${name}
431}
5de34b4c
JS
432
433port_get_description_title() {
434 assert [ $# -eq 1 ]
435
436 local name=${1}
437 description_title_read $(description_format_filename "port" "${name}")
438}